<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"chuangges.gitee.io","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="图片、二维码、Video 视频、Agora 声网">
<meta property="og:type" content="article">
<meta property="og:title" content="Web 图片、二维码、视频">
<meta property="og:url" content="https://chuangges.gitee.io/2019/03/02/2-4-web%E5%9B%BE%E5%83%8F/index.html">
<meta property="og:site_name" content="闯哥要去流浪了">
<meta property="og:description" content="图片、二维码、Video 视频、Agora 声网">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://chuangges.gitee.io/images/web/lazyload_1.gif">
<meta property="og:image" content="https://chuangges.gitee.io/images/web/lazyload_2.gif">
<meta property="og:image" content="https://chuangges.gitee.io/images/web/lazyload_3.png">
<meta property="og:image" content="https://chuangges.gitee.io/images/web/lazyload_4.gif">
<meta property="article:published_time" content="2019-03-02T13:02:57.000Z">
<meta property="article:modified_time" content="2023-06-09T08:47:10.020Z">
<meta property="article:author" content="chuangges">
<meta property="article:tag" content="前端技术栈">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://chuangges.gitee.io/images/web/lazyload_1.gif">

<link rel="canonical" href="https://chuangges.gitee.io/2019/03/02/2-4-web%E5%9B%BE%E5%83%8F/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Web 图片、二维码、视频 | 闯哥要去流浪了</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="闯哥要去流浪了" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">闯哥要去流浪了</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">愿你岁月可回首，且以深情共白头</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://chuangges.gitee.io/2019/03/02/2-4-web%E5%9B%BE%E5%83%8F/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/chuang.png">
      <meta itemprop="name" content="chuangges">
      <meta itemprop="description" content="chuangge's blog.">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="闯哥要去流浪了">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Web 图片、二维码、视频
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-03-02 21:02:57" itemprop="dateCreated datePublished" datetime="2019-03-02T21:02:57+08:00">2019-03-02</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2023-06-09 16:47:10" itemprop="dateModified" datetime="2023-06-09T16:47:10+08:00">2023-06-09</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%89%8D%E7%AB%AF%E6%8A%80%E6%9C%AF%E6%A0%88/" itemprop="url" rel="index"><span itemprop="name">前端技术栈</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>40k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>37 分钟</span>
            </span>
            <div class="post-description">图片、二维码、Video 视频、Agora 声网</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="一、Web-图片"><a href="#一、Web-图片" class="headerlink" title="一、Web 图片"></a>一、Web 图片</h1><h2 id="图片获取"><a href="#图片获取" class="headerlink" title="图片获取"></a>图片获取</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span>  H5 选择</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@method</span> changeImg        图片选中：获取 file 对象</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@method</span> fileReader       图片读取：file 转 base64</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@method</span> compress         图片压缩：缩放比例、图片质量等</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@method</span> dataURLtoFile    格式转换：base64 转 file</span></span><br><span class="line"><span class="comment"> * &lt;input type=&quot;file&quot; accept=&quot;image/*&quot; capture=&quot;camera&quot; <span class="doctag">@change</span>=&quot;changeImg&quot; /&gt;</span></span><br><span class="line"><span class="comment">**/</span></span><br><span class="line"><span class="keyword">async</span> <span class="title function_">changeImg</span>(<span class="params">e</span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> file = e.<span class="property">target</span>.<span class="property">files</span>[<span class="number">0</span>];</span><br><span class="line">    <span class="keyword">if</span> (!file) <span class="keyword">return</span>;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">base64Img</span> = <span class="keyword">await</span> <span class="variable language_">this</span>.<span class="title function_">fileReader</span>(file);</span><br><span class="line">&#125;,</span><br><span class="line"><span class="title function_">uploadFile</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="comment">// 截取 data:image/png;base64 后面的纯字符串、file 对象</span></span><br><span class="line">    <span class="keyword">let</span> baseImg = <span class="variable language_">this</span>.<span class="property">base64Img</span>.<span class="title function_">substring</span>(<span class="variable language_">this</span>.<span class="property">base64Img</span>.<span class="title function_">indexOf</span>(<span class="string">&quot;,&quot;</span>) + <span class="number">1</span>);</span><br><span class="line">    <span class="keyword">let</span> file = <span class="variable language_">this</span>.<span class="title function_">dataURLtoFile</span>(baseImg)</span><br><span class="line">    <span class="keyword">let</span> formData = <span class="keyword">new</span> <span class="title class_">FormData</span>();</span><br><span class="line">    formData.<span class="title function_">append</span>(<span class="string">&quot;image&quot;</span>, file);</span><br><span class="line">    <span class="title function_">axios</span>(&#123;</span><br><span class="line">        <span class="attr">method</span>: <span class="string">&#x27;post&#x27;</span>,</span><br><span class="line">        <span class="attr">url</span>: <span class="string">&quot;/unload&quot;</span>,</span><br><span class="line">        <span class="attr">data</span>: formData,</span><br><span class="line">        <span class="attr">headers</span>: &#123; <span class="string">&#x27;Content-Type&#x27;</span>: <span class="string">&quot;multipart/form-data&quot;</span> &#125;,</span><br><span class="line">        <span class="attr">responseType</span>: <span class="string">&quot;json&quot;</span></span><br><span class="line">    &#125;)</span><br><span class="line">&#125;,</span><br><span class="line"><span class="title function_">fileReader</span>(<span class="params">file, maxSize</span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> reader = <span class="keyword">new</span> <span class="title class_">FileReader</span>();</span><br><span class="line">    reader.<span class="title function_">readAsDataURL</span>(file);</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span></span><br><span class="line">        (reader.<span class="property">onloadend</span> = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">            <span class="title function_">resolve</span>(reader.<span class="property">result</span>);</span><br><span class="line">        &#125;)</span><br><span class="line">    )</span><br><span class="line">&#125;,</span><br><span class="line"><span class="title function_">compress</span>(<span class="params">baseUrl, file, maxSize = <span class="number">1</span>, scale = <span class="number">1</span>, quality = <span class="number">0.9</span></span>) &#123;</span><br><span class="line">    <span class="comment">// file.size 获取字节数：1MB = 1024KB，1KB = 1024字节</span></span><br><span class="line">    <span class="keyword">if</span> (file.<span class="property">size</span> &lt; maxSize * <span class="number">1024</span> * <span class="number">1024</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> baseUrl;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">let</span> img = <span class="keyword">new</span> <span class="title class_">Image</span>();</span><br><span class="line">    img.<span class="property">src</span> = baseUrl;</span><br><span class="line">    img.<span class="title function_">setAttribute</span>(<span class="string">&quot;crossOrigin&quot;</span>, <span class="string">&quot;Anonymous&quot;</span>); <span class="comment">// url 为外域时需要</span></span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span></span><br><span class="line">        (img.<span class="property">onload</span> = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">let</span> canvas = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&quot;canvas&quot;</span>),</span><br><span class="line">              ctx = canvas.<span class="title function_">getContext</span>(<span class="string">&quot;2d&quot;</span>),</span><br><span class="line">              w = img.<span class="property">width</span> * scale,</span><br><span class="line">              h = img.<span class="property">height</span> * scale;</span><br><span class="line">            canvas.<span class="property">width</span> = w;</span><br><span class="line">            canvas.<span class="property">height</span> = h;</span><br><span class="line">            ctx.<span class="title function_">drawImage</span>(img, <span class="number">0</span>, <span class="number">0</span>, w, h);</span><br><span class="line">            <span class="keyword">let</span> baseImg = canvas.<span class="title function_">toDataURL</span>(<span class="string">&quot;image/png&quot;</span>, quality)</span><br><span class="line">            <span class="title function_">resolve</span>(baseImg);</span><br><span class="line">        &#125;)</span><br><span class="line">    )</span><br><span class="line">&#125;,</span><br><span class="line"><span class="title function_">dataURLtoFile</span>(<span class="params">dataurl, filename</span>)&#123;</span><br><span class="line">    <span class="keyword">let</span> arr = dataurl.<span class="title function_">split</span>(<span class="string">&#x27;,&#x27;</span>), mime = arr[<span class="number">0</span>].<span class="title function_">match</span>(<span class="regexp">/:(.*?);/</span>)[<span class="number">1</span>],</span><br><span class="line">        bstr = <span class="title function_">atob</span>(arr[<span class="number">1</span>]), n = bstr.<span class="property">length</span>, u8arr = <span class="keyword">new</span> <span class="title class_">Uint8Array</span>(n);</span><br><span class="line">    <span class="keyword">while</span>(n--)&#123;</span><br><span class="line">        u8arr[n] = bstr.<span class="title function_">charCodeAt</span>(n);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">File</span>([u8arr], filename, &#123;<span class="attr">type</span>: mime&#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> Native 拍照：cordova plugin add cordova-plugin-camera</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> destinationType：DATA_URL 0、FILE_URI 1、NATIVE_URI 2</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> sourceType：CAMERA 相机、PHOTOLIBRARY SAVEDPHOTOALBUM 图库</span></span><br><span class="line"><span class="comment">**/</span></span><br><span class="line">navigator.<span class="property">camera</span>.<span class="title function_">getPicture</span>(onSuccess, onFail, &#123;</span><br><span class="line">    <span class="attr">quality</span>: <span class="number">100</span>,    <span class="comment">// 图像质量 1-100</span></span><br><span class="line">    <span class="attr">destinationType</span>: <span class="title class_">Camera</span>.<span class="property">DestinationType</span>.<span class="property">DATA_URL</span>, <span class="comment">// 返回格式</span></span><br><span class="line">    <span class="attr">sourceType</span>: <span class="title class_">Camera</span>.<span class="property">PictureSourceType</span>.<span class="property">CAMERA</span>,      <span class="comment">// 图片来源</span></span><br><span class="line">    <span class="attr">encodingType</span>: <span class="title class_">Camera</span>.<span class="property">EncodingType</span>.<span class="property">PNG</span>,  <span class="comment">// 返回编码：JPEG、PNG</span></span><br><span class="line">    <span class="attr">popoverOptions</span>: <span class="title class_">CameraPopoverOptions</span>,   <span class="comment">// iOS iPad 从相册选择</span></span><br><span class="line">    <span class="attr">correctOrientation</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="attr">saveToPhotoAlbum</span>: <span class="literal">false</span>,</span><br><span class="line">    <span class="attr">targetHeight</span>: <span class="number">600</span>,</span><br><span class="line">    <span class="attr">targetWidth</span>: <span class="number">600</span></span><br><span class="line">&#125;);</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">onSuccess</span>(<span class="params">baseImg</span>) &#123; </span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;data:image/png;base64,&quot;</span> + baseImg)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">onFail</span>(<span class="params">message</span>) &#123; &#125;</span><br></pre></td></tr></table></figure>


<h2 id="拼图验证"><a href="#拼图验证" class="headerlink" title="拼图验证"></a>拼图验证</h2>  <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">&quot;X-UA-Compatible&quot;</span> <span class="attr">content</span>=<span class="string">&quot;IE=edge&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Drag<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        * &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">padding</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">body</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">padding</span>: <span class="number">20px</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.check</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">400px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">300px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-repeat</span>: no-repeat;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-size</span>: <span class="number">100%</span> <span class="number">100%</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-image</span>: <span class="built_in">url</span>(<span class="string">https://img0.baidu.com/it/u=2028084904,</span></span></span><br><span class="line"><span class="string"><span class="language-css">            3939052004&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=889&amp;h=500</span>);</span></span><br><span class="line"><span class="language-css">            <span class="attribute">position</span>: relative;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.check</span><span class="selector-pseudo">::before</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">content</span>: <span class="string">&#x27;&#x27;</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">50px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">50px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background</span>: <span class="built_in">rgba</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0.5</span>);</span></span><br><span class="line"><span class="language-css">            <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#fff</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">position</span>: absolute;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">top</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">left</span>: <span class="number">280px</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.check-child</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">content</span>: <span class="string">&#x27;&#x27;</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">50px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">50px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#fff</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-image</span>: inherit;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-repeat</span>: inherit;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-size</span>: <span class="number">400px</span> <span class="number">300px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-position</span>: -<span class="number">280px</span> -<span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">position</span>: absolute;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">top</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">left</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.drag</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">400px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">50px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-color</span>: <span class="number">#e3e3e3</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin-top</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">position</span>: relative;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.drag-child</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">50px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">50px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-color</span>: aquamarine;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">z-index</span>: <span class="number">10</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">position</span>: absolute;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">top</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">left</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.drag-tips</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">display</span>: flex;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">align-items</span>: center;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">justify-content</span>: end;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">95%</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">100%</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">0</span> auto;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">font-size</span>: <span class="number">12px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">color</span>: <span class="number">#8a8a8a</span>;</span></span><br><span class="line"><span class="language-css">            user-select: none;     // 禁用文字选择</span></span><br><span class="line"><span class="language-css">            <span class="attribute">z-index</span>: <span class="number">1</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">position</span>: absolute;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">top</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">left</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">        <span class="keyword">@keyframes</span> move &#123;</span></span><br><span class="line"><span class="language-css">            <span class="selector-tag">to</span> &#123; <span class="attribute">transform</span>: <span class="built_in">translateX</span>(<span class="number">0</span>); &#125;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 图形校验区域 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;check&quot;</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 被校验区域 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;check-child&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 拖动条 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;drag&quot;</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 操作提示 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;drag-tips&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">span</span>&gt;</span>按住按钮向右拖动完成验证<span class="tag">&lt;/<span class="name">span</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 可拖动的盒子 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;drag-child&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">const</span> drag = <span class="variable language_">document</span>.<span class="title function_">querySelector</span>(<span class="string">&#x27;.drag-child&#x27;</span>)</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">const</span> check = <span class="variable language_">document</span>.<span class="title function_">querySelector</span>(<span class="string">&#x27;.check-child&#x27;</span>)</span></span><br><span class="line"><span class="language-javascript">    <span class="comment">// 鼠标按下</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">const</span> <span class="title function_">dragMouseDown</span> = event =&gt; &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">document</span>.<span class="title function_">addEventListener</span>(<span class="string">&#x27;mousemove&#x27;</span>, dragMouseMove)</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript">    <span class="comment">// 鼠标移动</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">const</span> <span class="title function_">dragMouseMove</span> = event =&gt; &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">const</span> &#123; pageX &#125; = event</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">if</span> (pageX &lt; <span class="number">0</span> || pageX &gt; <span class="number">350</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">return</span></span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">        drag.<span class="property">style</span>.<span class="property">transform</span> = <span class="string">`translateX(<span class="subst">$&#123;pageX&#125;</span>px)`</span></span></span><br><span class="line"><span class="language-javascript">        check.<span class="property">style</span>.<span class="property">transform</span> = <span class="string">`translateX(<span class="subst">$&#123;pageX&#125;</span>px)`</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">if</span> (pageX &gt;= <span class="number">278</span> &amp;&amp; pageX &lt;= <span class="number">285</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;通过校验&#x27;</span>);</span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript">    <span class="comment">// 鼠标松开</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">const</span> <span class="title function_">dragMouseUP</span> = event =&gt; &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">document</span>.<span class="title function_">removeEventListener</span>(<span class="string">&#x27;mousemove&#x27;</span>, dragMouseMove)</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">const</span> &#123; pageX &#125; = event</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">if</span> (pageX &gt;= <span class="number">278</span> &amp;&amp; pageX &lt;= <span class="number">285</span>) &#123; <span class="keyword">return</span> &#125;</span></span><br><span class="line"><span class="language-javascript">        drag.<span class="property">style</span>.<span class="property">animation</span> = <span class="string">&#x27;move 0.5s ease-in-out&#x27;</span></span></span><br><span class="line"><span class="language-javascript">        check.<span class="property">style</span>.<span class="property">animation</span> = <span class="string">&#x27;move 0.5s ease-in-out&#x27;</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">const</span> <span class="title function_">animateEnd</span> = (<span class="params"></span>)=&gt;&#123;</span></span><br><span class="line"><span class="language-javascript">            drag.<span class="property">style</span>.<span class="property">transform</span> = <span class="string">`translateX(<span class="subst">$&#123;<span class="number">0</span>&#125;</span>px)`</span></span></span><br><span class="line"><span class="language-javascript">            check.<span class="property">style</span>.<span class="property">transform</span> = <span class="string">`translateX(<span class="subst">$&#123;<span class="number">0</span>&#125;</span>px)`</span></span></span><br><span class="line"><span class="language-javascript">            drag.<span class="property">style</span>.<span class="property">animation</span> = <span class="string">&#x27;&#x27;</span></span></span><br><span class="line"><span class="language-javascript">            check.<span class="property">style</span>.<span class="property">animation</span> = <span class="string">&#x27;&#x27;</span></span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">document</span>.<span class="title function_">removeEventListener</span>(<span class="string">&quot;animationend&quot;</span>, animateEnd)</span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">document</span>.<span class="title function_">addEventListener</span>(<span class="string">&quot;animationend&quot;</span>, animateEnd)</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">document</span>.<span class="title function_">addEventListener</span>(<span class="string">&#x27;mousedown&#x27;</span>, dragMouseDown)</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">document</span>.<span class="title function_">addEventListener</span>(<span class="string">&#x27;mouseup&#x27;</span>, dragMouseUP)</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>


<h2 id="图片懒加载"><a href="#图片懒加载" class="headerlink" title="图片懒加载"></a>图片懒加载</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1、背景色占位过渡：提取图片的主色调则可借助 color-thief</span></span><br><span class="line"><span class="keyword">const</span> img = <span class="keyword">new</span> <span class="title class_">Image</span>()</span><br><span class="line">img.<span class="property">src</span> = <span class="string">&#x27;./dog.jpeg&#x27;</span></span><br><span class="line">img.<span class="title function_">setAttribute</span>(<span class="string">&#x27;width&#x27;</span>, <span class="number">1</span>)</span><br><span class="line">img.<span class="title function_">setAttribute</span>(<span class="string">&#x27;height&#x27;</span>, <span class="number">1</span>)</span><br><span class="line">img.<span class="property">onload</span> = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> canvas = <span class="keyword">new</span> <span class="title class_">OffscreenCanvas</span>(<span class="number">1</span>, <span class="number">1</span>)</span><br><span class="line">    <span class="keyword">const</span> ctx = canvas.<span class="title function_">getContext</span>(<span class="string">&#x27;2d&#x27;</span>)</span><br><span class="line">    ctx.<span class="title function_">drawImage</span>(img, <span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>)</span><br><span class="line">    <span class="keyword">const</span> &#123; data &#125; = ctx.<span class="title function_">getImageData</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>)</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`rgba(<span class="subst">$&#123;data.join(<span class="string">&#x27;,&#x27;</span>)&#125;</span>)`</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 2、LQIP：webp 低质量图像占位、real-src 按需加载</span></span><br><span class="line">.<span class="property">container</span> &#123;</span><br><span class="line">    <span class="attr">position</span>: relative;</span><br><span class="line">&#125;</span><br><span class="line">.<span class="property">container</span> img &#123;</span><br><span class="line">    <span class="attr">position</span>: absolute;</span><br><span class="line">    <span class="attr">width</span>: <span class="number">100</span>%;</span><br><span class="line">&#125;</span><br><span class="line">.<span class="property">hidden</span> &#123;</span><br><span class="line">    <span class="attr">opacity</span>: <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line">.<span class="property">pre</span> &#123;</span><br><span class="line">    <span class="attr">opacity</span>: <span class="number">1</span>;</span><br><span class="line">    <span class="attr">filter</span>: <span class="title function_">blur</span>(10px);</span><br><span class="line">    <span class="attr">transition</span>: filter 2s ease-<span class="keyword">in</span>-out;</span><br><span class="line">&#125;</span><br><span class="line">.<span class="property">pre</span>.<span class="property">loaded</span> &#123;</span><br><span class="line">    <span class="attr">filter</span>: <span class="title function_">blur</span>(<span class="number">0</span>);</span><br><span class="line">&#125;</span><br><span class="line">.<span class="property">next</span> &#123;</span><br><span class="line">    <span class="attr">opacity</span>: <span class="number">0</span>;</span><br><span class="line">    <span class="attr">transition</span>: opacity 2s ease-<span class="keyword">in</span>-out;</span><br><span class="line">&#125;</span><br><span class="line">.<span class="property">next</span>.<span class="property">loaded</span> &#123;</span><br><span class="line">    <span class="attr">opacity</span>: <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line">&lt;div <span class="keyword">class</span>=<span class="string">&quot;container&quot;</span>&gt;</span><br><span class="line">    &lt;img class=&quot;pre&quot; src=&quot;yasuo.webp&quot; alt=&quot;&quot;&gt;</span><br><span class="line">    &lt;img class=&quot;next&quot; real-src=&quot;yasuo.png&quot; alt=&quot;&quot;&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">setTimeout(() =&gt; &#123;</span><br><span class="line">    const pre = document.querySelector(&#x27;.pre&#x27;)</span><br><span class="line">    const next = document.querySelector(&#x27;.next&#x27;)</span><br><span class="line">    next.src = &#x27;yasuo.png&#x27;</span><br><span class="line">    pre.classList.add(&#x27;loaded&#x27;)</span><br><span class="line">    next.classList.add(&#x27;loaded&#x27;)</span><br><span class="line">    next.addEventListener(&#x27;transitionend&#x27;, function animationend() &#123;</span><br><span class="line">        pre.classList.add(&#x27;hidden&#x27;)</span><br><span class="line">        next.removeEventListener(&#x27;transitionend&#x27;, animationend)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;, 2000)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">// 3、SQIP：根据需要生成矢量的 SVG 轮廓</span><br><span class="line">const sqip = require(&#x27;sqip&#x27;)</span><br><span class="line">const res = sqip(&#123;</span><br><span class="line">    filename: &#x27;yasuo.png&#x27;,</span><br><span class="line">    numberOfPrimitives: 200,</span><br><span class="line">    blur: 0</span><br><span class="line">&#125;)</span><br><span class="line">console.log(res.final_svg)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">// 4、剪影过渡：生成 svg、实现剪影</span><br><span class="line">const potrace = require(&#x27;potrace&#x27;)</span><br><span class="line">const fs = require(&#x27;fs&#x27;)</span><br><span class="line">const params = &#123;</span><br><span class="line">    background: &#x27;#fff&#x27;,</span><br><span class="line">    color: &#x27;#c7d4d8&#x27;,</span><br><span class="line">    threshold: 120</span><br><span class="line">&#125;;</span><br><span class="line">potrace.trace(&#x27;./yasuo.png&#x27;, params, function(err, svg) &#123;</span><br><span class="line">    fs.writeFileSync(&#x27;./yasuo.svg&#x27;, svg);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">.pre &#123;</span><br><span class="line">    opacity: 1;</span><br><span class="line">    transition: filter 2s ease-in-out;</span><br><span class="line">&#125;</span><br><span class="line">.next &#123;</span><br><span class="line">    opacity: 0;</span><br><span class="line">    transition: opacity 2s ease-in-out;</span><br><span class="line">&#125;</span><br><span class="line">.next.loaded &#123;</span><br><span class="line">    opacity: 1;</span><br><span class="line">&#125;</span><br><span class="line">&lt;div class=&quot;container&quot;&gt;</span><br><span class="line">    &lt;img class=&quot;pre&quot; src=&quot;yasuo.svg&quot; alt=&quot;&quot;&gt;</span><br><span class="line">    &lt;img class=&quot;next&quot; real-src=&quot;yasuo.png&quot; alt=&quot;&quot;&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">setTimeout(() =&gt; &#123;</span><br><span class="line">    const pre = document.querySelector(&#x27;.pre&#x27;)</span><br><span class="line">    const next = document.querySelector(&#x27;.next&#x27;)</span><br><span class="line">    next.src = &#x27;yasuo.png&#x27;</span><br><span class="line">    next.classList.add(&#x27;loaded&#x27;)</span><br><span class="line">    next.addEventListener(&#x27;transitionend&#x27;, function animationend() &#123;</span><br><span class="line">        pre.classList.add(&#x27;hidden&#x27;)</span><br><span class="line">        next.removeEventListener(&#x27;transitionend&#x27;, animationend)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;, 2000)</span><br></pre></td></tr></table></figure>

  <div align="center">
      <img src="" data-original="/images/web/lazyload_1.gif" alt="懒加载" width="100%">
      <img src="" data-original="/images/web/lazyload_2.gif" alt="懒加载" width="100%">
      <img src="" data-original="/images/web/lazyload_3.png" alt="懒加载" width="100%">
      <img src="" data-original="/images/web/lazyload_4.gif" alt="懒加载" width="100%">
  </div>


<h2 id="ThreeJS-绘图"><a href="#ThreeJS-绘图" class="headerlink" title="ThreeJS 绘图"></a>ThreeJS 绘图</h2><ul>
<li><strong>CPU</strong>：运算效率低的万能教授，处理各种不同数据和逻辑判断的中央处理器。</li>
<li><strong>GPU</strong>：执行并发运算效率高的专业学生，专门执行复杂数学计算的图形处理单元。</li>
<li><strong>OpenGL</strong>：最常用的跨平台图形库，用于渲染矢量图形的跨语言跨平台的应用接口。</li>
<li><strong>OpenGL ES</strong>：OpenGL API 子集，是针对手机和游戏主机等嵌入式设备而设计的。</li>
<li><strong>GLSL</strong>：OpenGL ES 着色语言，图形卡 GPU 上执行并实现视图转换等渲染管线功能。</li>
<li><strong>WebGL</strong>：基于 OpenGL 面向 web 的图形标准，提供了一系列 JS API 可用于控制 GPU 渲染管线 (用于接收 CPU 传送的顶点和纹理等信息并处理为一张二维图像的显卡硬件)。</li>
<li><strong>ThreeJS</strong>：一款封装 webGL 复杂接口的框架，简化工作有模型数据、各种矩阵、着色器等。</li>
<li><strong>坐标系</strong>：GPU WebGL 主要用于将顶点数据通过矩阵变换转换到裁剪空间坐标，然后基于空间坐标绘制像素点，无论画布多大都会将坐标限制到 (1, 1, 1)、(-1, -1, -1)。</li>
<li><strong>缓冲区对象</strong>：GPU 存储顶点和颜色等绘制模型的二进制数据。</li>
<li><strong>图元</strong>：几何顶点的组合 (点、线段和三角面)，一般是三角形。</li>
<li><strong>网格</strong>：使用顶点所绘的 2D、3D 模型，每个面被称为一个片元 (一般三角形)。</li>
<li><strong>片元和像素</strong>：光栅化的纹理映射后图元转为像素，片元指包括颜色纹理等的像素点像素点。</li>
<li><strong>着色器</strong>：GPU 顶点处理和片段处理等可编程部分，主要包括顶点和片元着色器。着色器程序使用 GLSL 语言编写并以字符串形式存于 JS 等待编译处理后传递给着色器执行。</li>
<li><strong>纹理图像</strong>：将一张真实图片贴到一个几何图像表面称为 纹理映射，映射的图像为 纹理图像，纹理图像的组成像素和坐标则分别被称为 纹素、纹理坐标 (可用于获取纹素颜色)。</li>
<li><strong>绘图</strong>：WebGL 只能绘制 点、线和三角形，将它们合成各种图形后绘制到三维空间。顶点、颜色、纹理等几何信息都存于缓冲区对象并被传递到着色器程序来创建图形对象。</li>
<li><strong>图像绘制流程</strong>：浏览器解析服务器硬盘的 WebGL 文件后由 CPU 将顶点数据和着色程序传递到 GPU 显存，CPU 调用着色程序初始化时创建的顶点&#x2F;片段着色器和顶点数据传递到 GPU 处理器、GPU 通过矩阵变换和光栅化等操作将图像信息转化为像素并写入显卡等待读取。</li>
<li><strong>GPU 流程</strong>：矩阵变换 – GPU 逐顶点执行顶点着色器而通过矩阵变换将 3D <code>顶点坐标</code>转换为 2D <code>图元</code>、图元装配 – 几何着色器通过新顶点裁剪图元并映射为<code>像素</code>、光栅化 – GPU 逐片元执行片元着色器生成像素着色后的<code>片元</code>、检测融合 – 检测片元的深度和透明度并混合。</li>
</ul>
  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> 绘制一个几何体</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> scene 场景：构建一个三维空间，是物体、光源等元素的容器 。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> camera 相机：选择一个观察点，控制视角位置、范围和焦点位置。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> Object3D 物体：包括二维的点线面、三维的粒子模型等。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> renderer 渲染器：渲染场景及其观察物体的画布。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> Light 光源：包括全局光、平行光、点光源等。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> Control 控制器：可通过键盘、鼠标控制相机的控件。</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> 相机分类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> OrthographicCamera 正投影相机：物体实际尺寸而远近相同。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> PerspectiveCamera  透视投影相机：类似人眼而近大远小。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> StereoCamera 3D相机：合成两个相机效果实现立体影像或视差屏障等。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> CubeCamera 全景相机：可以 360 度拍摄。</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> 物体分类：基类为 Object3D，参数为定义形状和表面效果的几何体和材质。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> Line 线段模型：由线条组成。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> Mesh 网格模型：由三角面和四边形面组成。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> Points 粒子系统：使用一堆点的集合来描述。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> Sprite 精灵图：用于加载纹理的二维图像或动画 (不接受阴影)。</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> 光源分类：基类为 Light，环境光和方向光的光强不受距离远近影响。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> AmbientLight 环境光：各个方向均匀反射到物体表面，用于弱化阴影和添加颜色。</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> DirectionLight 平行光：类似太阳光一组没有衰减的平行光线，用于实现阴影</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> PointLight 点光源：单点发光但辐射四面八方，类似蜡烛和灯泡</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> SpotLight 聚光灯：从一个锥体中射出而产生聚光效果，类似台灯和手电筒</span></span><br><span class="line"><span class="comment">**/</span> </span><br><span class="line"><span class="keyword">function</span> <span class="title function_">initGrid</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">var</span> scene = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Scene</span>();</span><br><span class="line">  <span class="comment">// 正投影相机、位置、方向、面向坐标</span></span><br><span class="line">  <span class="keyword">var</span> camera = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">OrthographicCamera</span>(-<span class="number">5</span>, <span class="number">5</span>, <span class="number">3.75</span>, -<span class="number">3.75</span>, <span class="number">0.1</span>, <span class="number">10</span>);</span><br><span class="line">  camera.<span class="property">position</span>.<span class="title function_">set</span>(<span class="number">0</span>, -<span class="number">25</span>, <span class="number">0</span>);</span><br><span class="line">  camera.<span class="title function_">lookAt</span>(scene.<span class="property">position</span>);               <span class="comment">// 默认面向 z 轴负方向</span></span><br><span class="line">  camera.<span class="title function_">lookAt</span>(<span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(<span class="number">10</span>, <span class="number">0</span>, <span class="number">0</span>));  <span class="comment">// 相机往x轴移动10单位 </span></span><br><span class="line">  scene.<span class="title function_">add</span>(camera);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">var</span> renderer = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">WebGLRenderer</span>(&#123; <span class="attr">antialias</span>: <span class="literal">true</span> &#125;);</span><br><span class="line">  renderer.<span class="title function_">setSize</span>(<span class="number">800</span>, <span class="number">600</span>);</span><br><span class="line">  renderer.<span class="property">autoClear</span> = <span class="literal">false</span>;</span><br><span class="line">  renderer.<span class="title function_">setClearColor</span>(<span class="number">0x000000</span>);</span><br><span class="line">  <span class="variable language_">document</span>.<span class="property">body</span>.<span class="title function_">appendChild</span>(renderer.<span class="property">domElement</span>);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">var</span> geometry = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Geometry</span>();</span><br><span class="line">  geometry.<span class="property">vertices</span>.<span class="title function_">push</span>(<span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(-<span class="number">2</span>, <span class="number">0</span>, <span class="number">0</span>));</span><br><span class="line">  geometry.<span class="property">vertices</span>.<span class="title function_">push</span>(<span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(<span class="number">2</span>, <span class="number">0</span>, <span class="number">0</span>));</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt;= <span class="number">5</span>; i++) &#123;</span><br><span class="line">      <span class="comment">// 定义竖着的线段</span></span><br><span class="line">      <span class="keyword">var</span> line = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Line</span>(geometry, <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">LineBasicMaterial</span>(&#123;</span><br><span class="line">          <span class="attr">color</span>: <span class="number">0xffffff</span>,</span><br><span class="line">      &#125;))</span><br><span class="line">      <span class="comment">// 线段间隔为 0.8，-2 达到田字格效果</span></span><br><span class="line">      line.<span class="property">position</span>.<span class="property">z</span> = (i * <span class="number">0.8</span>) - <span class="number">2</span>;</span><br><span class="line">      scene.<span class="title function_">add</span>(line);</span><br><span class="line">      <span class="comment">// 定义横着的线段</span></span><br><span class="line">      <span class="keyword">var</span> line = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Line</span>(geometry, <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">LineBasicMaterial</span>(&#123;</span><br><span class="line">          <span class="attr">color</span>: <span class="number">0xffffff</span>,</span><br><span class="line">          <span class="attr">opacity</span>: <span class="number">0.2</span></span><br><span class="line">      &#125;))</span><br><span class="line">      line.<span class="property">position</span>.<span class="property">x</span> = (i * <span class="number">0.8</span>) - <span class="number">2</span>;</span><br><span class="line">      line.<span class="property">rotation</span>.<span class="property">y</span> = <span class="number">90</span> * <span class="title class_">Math</span>.<span class="property">PI</span> / <span class="number">180</span>;</span><br><span class="line">      scene.<span class="title function_">add</span>(line);</span><br><span class="line">      renderer.<span class="title function_">render</span>(scene, camera);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> 绘制一个旋转的正方体</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> 物体移动：scale、rotation、position、material</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> 摄像机移动：position、lookAt、up、target</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> TweenJS：可定义属性值的过渡而实现复杂动画，自动计算中间值(补间)</span></span><br><span class="line"><span class="comment">**/</span></span><br><span class="line"><span class="keyword">var</span> scene, camera, renderer, mesh, stats;</span><br><span class="line"><span class="title function_">initThree</span>();</span><br><span class="line"><span class="title function_">animation</span>();</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">initThree</span>(<span class="params"></span>) &#123;</span><br><span class="line">    scene = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Scene</span>();  </span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> aspect = <span class="variable language_">window</span>.<span class="property">innerWidth</span> / <span class="variable language_">window</span>.<span class="property">innerHeight</span>;</span><br><span class="line">    camera = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">PerspectiveCamera</span>( <span class="number">70</span>, aspect, <span class="number">1</span>, <span class="number">1000</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 光照设置</span></span><br><span class="line">    <span class="keyword">var</span> light = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">DirectionalLight</span>( <span class="number">0xffffff</span> );</span><br><span class="line">    light.<span class="property">position</span>.<span class="title function_">set</span>( <span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span> ).<span class="title function_">normalize</span>();</span><br><span class="line">    scene.<span class="title function_">add</span>(light);    <span class="comment">// 将光照插入场景</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 创建：立方体几何对象、材质对象、网络模型</span></span><br><span class="line">    <span class="keyword">var</span> geometry = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">BoxGeometry</span>( <span class="number">10</span>, <span class="number">10</span>, <span class="number">10</span>);</span><br><span class="line">    <span class="keyword">var</span> material = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">MeshPhongMaterial</span>(&#123; </span><br><span class="line">        <span class="attr">color</span>: <span class="number">0x0033ff</span>, </span><br><span class="line">        <span class="attr">specular</span>: <span class="number">0x555555</span>, </span><br><span class="line">        <span class="attr">shininess</span>: <span class="number">30</span> </span><br><span class="line">    &#125;);</span><br><span class="line">    mesh = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Mesh</span>(geometry, material );</span><br><span class="line">    mesh.<span class="property">position</span>.<span class="property">z</span> = -<span class="number">50</span>;</span><br><span class="line">    scene.<span class="title function_">add</span>(mesh);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 创建渲染器对象</span></span><br><span class="line">    renderer = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">WebGLRenderer</span>(&#123; <span class="attr">antialias</span>: <span class="literal">true</span> &#125;)</span><br><span class="line">    renderer.<span class="title function_">setSize</span>(<span class="variable language_">window</span>.<span class="property">innerWidth</span>, <span class="variable language_">window</span>.<span class="property">innerHeight</span>)</span><br><span class="line">    renderer.<span class="title function_">clear</span>()                        </span><br><span class="line">    renderer.<span class="title function_">setClearColor</span>(<span class="number">0xb9d3ff</span>, <span class="number">1</span>)</span><br><span class="line">    <span class="variable language_">document</span>.<span class="property">body</span>.<span class="title function_">appendChild</span>(renderer.<span class="property">domElement</span>)</span><br><span class="line"></span><br><span class="line">    <span class="title function_">render</span>(); </span><br><span class="line">    <span class="variable language_">window</span>.<span class="title function_">addEventListener</span>( <span class="string">&#x27;resize&#x27;</span>, onWindowResize, <span class="literal">false</span> );</span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">onWindowResize</span>(<span class="params"></span>) &#123;</span><br><span class="line">        camera.<span class="property">aspect</span> = <span class="variable language_">window</span>.<span class="property">innerWidth</span> / <span class="variable language_">window</span>.<span class="property">innerHeight</span>;</span><br><span class="line">        camera.<span class="title function_">updateProjectionMatrix</span>();</span><br><span class="line">        renderer.<span class="title function_">setSize</span>( <span class="variable language_">window</span>.<span class="property">innerWidth</span>, <span class="variable language_">window</span>.<span class="property">innerHeight</span> );</span><br><span class="line">        <span class="title function_">render</span>();</span><br><span class="line">    &#125;       </span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">render</span>(<span class="params"></span>) &#123;</span><br><span class="line">    renderer.<span class="title function_">render</span>(scene, camera)    </span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">animation</span>(<span class="params"></span>) &#123;</span><br><span class="line">    mesh.<span class="property">rotation</span>.<span class="property">x</span> += <span class="number">.04</span>;</span><br><span class="line">    camera.<span class="property">position</span>.<span class="property">x</span> = camera.<span class="property">position</span>.<span class="property">x</span> + <span class="number">1</span></span><br><span class="line">    <span class="title function_">render</span>();</span><br><span class="line">    <span class="title function_">requestAnimationFrame</span>(animation);</span><br><span class="line">    <span class="comment">// TWEEN.update();    复杂动画</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">initTween</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="keyword">new</span> <span class="variable constant_">TWEEN</span>.<span class="title class_">Tween</span>(mesh.<span class="property">position</span>)     <span class="comment">// 要改变属性的对象</span></span><br><span class="line">             .<span class="title function_">to</span>( &#123; <span class="attr">x</span>: -<span class="number">400</span> &#125;, <span class="number">3000</span> )  <span class="comment">// 目标值和需要时间</span></span><br><span class="line">             .<span class="title function_">repeat</span>( <span class="title class_">Infinity</span> )       <span class="comment">// 重复无穷次</span></span><br><span class="line">             .<span class="title function_">start</span>();                 <span class="comment">// 开始动画</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建三维物体：几何体对象和材质对象</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">initShape</span>(<span class="params"></span>)&#123;</span><br><span class="line">  <span class="keyword">var</span> geometry, material;   </span><br><span class="line"></span><br><span class="line">  <span class="comment">// -------------- 构造函数创建 ---------------</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 矩形平面：PlanGeometry(长,宽, 长的分割, 宽的分割) </span></span><br><span class="line">  <span class="keyword">let</span> planeGeometry = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">PlaneGeometry</span>(<span class="number">100</span>, <span class="number">50</span>)</span><br><span class="line">  <span class="keyword">let</span> planeMaterial = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">MeshLambertMaterial</span>(&#123; <span class="attr">color</span>: <span class="number">0xffffff</span> &#125;)</span><br><span class="line">  <span class="keyword">let</span> plane = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Mesh</span>(planeGeometry,planeMaterial)</span><br><span class="line">  plane.<span class="property">receiveShadow</span> = <span class="literal">true</span>        <span class="comment">// 接收阴影</span></span><br><span class="line">  plane.<span class="property">rotation</span>.<span class="property">x</span> = -<span class="number">0.5</span>*<span class="title class_">Math</span>.<span class="property">PI</span>   <span class="comment">// x 轴旋转 -90 度</span></span><br><span class="line">  plane.<span class="property">position</span>.<span class="title function_">set</span>(<span class="number">15</span>, <span class="number">0</span>, <span class="number">0</span>)</span><br><span class="line">  scene.<span class="title function_">add</span>( plane )</span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">  <span class="comment">// 圆平面：CircleGeometry(半径, 切片数, 开始, 跨过角度)  </span></span><br><span class="line">  <span class="comment">// 切片数越大则越接近圆, 参数为3(最小值)是三角形, 参数为4则是矩形</span></span><br><span class="line">  geometry = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">CircleGeometry</span>(<span class="number">3</span>, <span class="number">100</span>)</span><br><span class="line">  material = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">MeshLambertMaterial</span>(&#123; <span class="attr">color</span>: <span class="number">0xffffff</span> &#125;)</span><br><span class="line">  <span class="keyword">let</span> circle = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Mesh</span>(geometry, material)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 圆环：TorusGeometry(半径, 管道半径, 纬度分割, 经度分割, 圆环面的弧度)</span></span><br><span class="line">  <span class="comment">// 最后一个参数 控制是否绘制完整一个圆环</span></span><br><span class="line">  geometry = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">TorusGeometry</span>(<span class="number">5</span>, <span class="number">1</span>, <span class="number">50</span>, <span class="number">50</span>)  </span><br><span class="line">  material = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">MeshLambertMaterial</span>(&#123; <span class="attr">color</span>: <span class="number">0x7777ff</span> &#125;)</span><br><span class="line">  <span class="keyword">let</span> torus = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Mesh</span>(geometry, material)</span><br><span class="line"></span><br><span class="line">  </span><br><span class="line">  <span class="comment">// 立方体：BoxGeometry(长, 宽, 高, 长的分割, 宽的分割, 高的分割)</span></span><br><span class="line">  geometry = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">BoxGeometry</span>(<span class="number">4</span>, <span class="number">4</span>, <span class="number">4</span>)</span><br><span class="line">  material = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">MeshLambertMaterial</span>(&#123; <span class="attr">color</span>: <span class="number">0xff0000</span> &#125;)</span><br><span class="line">  <span class="keyword">let</span> box = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Mesh</span>(geometry, material)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  <span class="comment">// -------------- 自定义创建 ---------------</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 点模型</span></span><br><span class="line">  geometry = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Geometry</span>();        <span class="comment">// 声明一个空几何体对象</span></span><br><span class="line">  <span class="keyword">var</span> p1 = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(<span class="number">10</span>, <span class="number">0</span>, <span class="number">0</span>);   <span class="comment">// 顶点1坐标</span></span><br><span class="line">  <span class="keyword">var</span> p2 = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(<span class="number">0</span>, <span class="number">20</span>, <span class="number">0</span>);   <span class="comment">// 顶点2坐标</span></span><br><span class="line">  <span class="keyword">var</span> p3 = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(<span class="number">15</span>, <span class="number">15</span>, <span class="number">0</span>);  <span class="comment">// 顶点3坐标</span></span><br><span class="line">  geometry.<span class="property">vertices</span>.<span class="title function_">push</span>(p1,p2,p3);       <span class="comment">// 添加到对象</span></span><br><span class="line"></span><br><span class="line">  material = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">PointsMaterial</span>(&#123;   <span class="comment">// 点对象像素的颜色尺寸</span></span><br><span class="line">      <span class="attr">color</span>: <span class="number">0x0000ff</span>,</span><br><span class="line">      <span class="attr">size</span>: <span class="number">10.0</span>       </span><br><span class="line">  &#125;); </span><br><span class="line">  <span class="keyword">var</span> points = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Points</span>(geometry, material);</span><br><span class="line">  scene.<span class="title function_">add</span>(points);        <span class="comment">// 点模型添加到场景中</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 线条</span></span><br><span class="line">  geometry = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Geometry</span>();</span><br><span class="line">  <span class="keyword">var</span> p1 = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(<span class="number">10</span>, <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">  <span class="keyword">var</span> p2 = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(<span class="number">0</span>, <span class="number">20</span>, <span class="number">0</span>);</span><br><span class="line">  geometry.<span class="property">vertices</span>.<span class="title function_">push</span>(p1, p2);           <span class="comment">// vertices 存放坐标</span></span><br><span class="line">  </span><br><span class="line">  <span class="keyword">var</span> color1 = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Color</span>(<span class="number">0xFF0000</span>);  </span><br><span class="line">  <span class="keyword">var</span> color2 = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Color</span>(<span class="number">0x0000FF</span>);   </span><br><span class="line">  geometry.<span class="property">colors</span>.<span class="title function_">push</span>(color1, color2);      <span class="comment">// colors 存放顶点颜色</span></span><br><span class="line">  </span><br><span class="line">  material = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">LineBasicMaterial</span>(&#123;</span><br><span class="line">      <span class="attr">color</span>: <span class="number">0x0000ff</span>,                       <span class="comment">// 线条颜色，用16进制表示</span></span><br><span class="line">      <span class="attr">vertexColors</span>: <span class="variable constant_">THREE</span>.<span class="property">VertexColors</span>,      <span class="comment">// 线条材质是否使用顶点颜色</span></span><br><span class="line">  &#125;);</span><br><span class="line">  <span class="keyword">var</span> line = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Line</span>(geometry, material);</span><br><span class="line">  scene.<span class="title function_">add</span>(line);  </span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 三角面和矩形 </span></span><br><span class="line">  geometry = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Geometry</span>(); </span><br><span class="line">  <span class="keyword">var</span> p1 = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>); </span><br><span class="line">  <span class="keyword">var</span> p2 = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(<span class="number">80</span>, <span class="number">0</span>, <span class="number">0</span>); </span><br><span class="line">  <span class="keyword">var</span> p3 = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(<span class="number">0</span>, <span class="number">80</span>, <span class="number">0</span>); </span><br><span class="line"></span><br><span class="line">  <span class="comment">// 创建三角面及其法向量 (平面的法向量垂直于平面本身)</span></span><br><span class="line">  geometry.<span class="property">vertices</span>.<span class="title function_">push</span>(p1, p2, p3); </span><br><span class="line">  <span class="keyword">var</span> normal = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>( <span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span> ); </span><br><span class="line">  <span class="keyword">var</span> face = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Face3</span>( <span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, normal); </span><br><span class="line">  <span class="keyword">var</span> color1 = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Color</span>(<span class="number">0xFF0000</span>);  <span class="comment">// 红色</span></span><br><span class="line">  <span class="keyword">var</span> color2 = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Color</span>(<span class="number">0x00FF00</span>);  <span class="comment">// 绿色</span></span><br><span class="line">  <span class="keyword">var</span> color3 = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Color</span>(<span class="number">0x0000FF</span>);  <span class="comment">// 蓝色</span></span><br><span class="line">  face.<span class="property">vertexColors</span>.<span class="title function_">push</span>(color1, color2, color3);  <span class="comment">// 定义顶点颜色</span></span><br><span class="line">  geometry.<span class="property">faces</span>.<span class="title function_">push</span>(face); </span><br><span class="line"></span><br><span class="line">  <span class="comment">// 创建矩形平面</span></span><br><span class="line">  <span class="keyword">var</span> p4 = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(<span class="number">0</span>, <span class="number">80</span>, <span class="number">0</span>); </span><br><span class="line">  geometry.<span class="property">vertices</span>.<span class="title function_">push</span>(p1, p2, p3, p4); </span><br><span class="line">  <span class="keyword">var</span> normal = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>); </span><br><span class="line">  <span class="keyword">var</span> face0 = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Face3</span>(<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, normal); </span><br><span class="line">  <span class="keyword">var</span> face1 = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Face3</span>(<span class="number">0</span>, <span class="number">2</span>, <span class="number">3</span>, normal); </span><br><span class="line">  geometry.<span class="property">faces</span>.<span class="title function_">push</span>(face0, face1); </span><br><span class="line"></span><br><span class="line">  material = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">MeshLambertMaterial</span>(&#123;</span><br><span class="line">      <span class="attr">color</span>: <span class="number">0x0000ff</span>,         </span><br><span class="line">      <span class="attr">side</span>: <span class="variable constant_">THREE</span>.<span class="property">DoubleSide</span>   <span class="comment">// 两面可见</span></span><br><span class="line">  &#125;);</span><br><span class="line">  <span class="keyword">var</span> mesh = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Mesh</span>(geometry,material);</span><br><span class="line">  scene.<span class="title function_">add</span>(mesh);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 二维图形：ShapeGeometry(shapes, options)  </span></span><br><span class="line">  <span class="comment">// shapes 参数: THREE.Shape对象可单个传入, 多个时传数组</span></span><br><span class="line">  <span class="keyword">var</span> x = <span class="number">0</span>, y = <span class="number">0</span></span><br><span class="line">  <span class="keyword">var</span> heartShape = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Shape</span>()  <span class="comment">// 心形</span></span><br><span class="line"></span><br><span class="line">  heartShape.<span class="title function_">moveTo</span>(x + <span class="number">5</span>, y + <span class="number">5</span>)</span><br><span class="line">  heartShape.<span class="title function_">bezierCurveTo</span>(x + <span class="number">5</span>, y + <span class="number">5</span>, x + <span class="number">4</span>, y, x, y)</span><br><span class="line">  heartShape.<span class="title function_">bezierCurveTo</span>(x - <span class="number">6</span>, y, x - <span class="number">6</span>, y + <span class="number">7</span>,x - <span class="number">6</span>, y + <span class="number">7</span>)</span><br><span class="line">  heartShape.<span class="title function_">bezierCurveTo</span>(x - <span class="number">6</span>, y + <span class="number">11</span>, x - <span class="number">3</span>, y + <span class="number">15.4</span>, x + <span class="number">5</span>, y + <span class="number">19</span>)</span><br><span class="line">  heartShape.<span class="title function_">bezierCurveTo</span>(x + <span class="number">12</span>, y + <span class="number">15.4</span>, x + <span class="number">16</span>, y + <span class="number">11</span>, x + <span class="number">16</span>, y+<span class="number">7</span>)</span><br><span class="line">  heartShape.<span class="title function_">bezierCurveTo</span>(x + <span class="number">16</span>, y + <span class="number">7</span>, x + <span class="number">16</span>, y, x + <span class="number">10</span>, y)</span><br><span class="line">  heartShape.<span class="title function_">bezierCurveTo</span>(x + <span class="number">7</span>, y, x + <span class="number">5</span>, y + <span class="number">5</span>, x + <span class="number">5</span>, y + <span class="number">5</span>)</span><br><span class="line"></span><br><span class="line">  geometry = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">ShapeGeometry</span>( heartShape )</span><br><span class="line">  material = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">MeshBasicMaterial</span>( &#123; <span class="attr">color</span>: <span class="number">0x00ff00</span> &#125; )</span><br><span class="line">  <span class="keyword">var</span> mesh = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Mesh</span>( geometry, material ) </span><br><span class="line">  scene.<span class="title function_">add</span>( mesh )</span><br><span class="line"></span><br><span class="line">    </span><br><span class="line">  <span class="comment">// 几何体 </span></span><br><span class="line">  <span class="comment">// 1.创建顶点</span></span><br><span class="line">  <span class="keyword">var</span> vertices = [</span><br><span class="line">      <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(<span class="number">0</span>,<span class="number">2</span>,<span class="number">0</span>),</span><br><span class="line">      <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(-<span class="number">1</span>,<span class="number">0</span>,<span class="number">0</span>),</span><br><span class="line">      <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(<span class="number">0</span>,<span class="number">0</span>,<span class="number">1</span>),</span><br><span class="line">      <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(<span class="number">1</span>,<span class="number">0</span>,<span class="number">0</span>),</span><br><span class="line">      <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(<span class="number">0</span>,<span class="number">0</span>,-<span class="number">1</span>),</span><br><span class="line">      <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Vector3</span>(<span class="number">0</span>,-<span class="number">2</span>,<span class="number">0</span>)</span><br><span class="line">  ]</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 2.创建面</span></span><br><span class="line">  <span class="keyword">var</span> faces = [</span><br><span class="line">      <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Face3</span>(<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>),</span><br><span class="line">      <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Face3</span>(<span class="number">0</span>,<span class="number">2</span>,<span class="number">3</span>),</span><br><span class="line">      <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Face3</span>(<span class="number">0</span>,<span class="number">3</span>,<span class="number">4</span>),</span><br><span class="line">      <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Face3</span>(<span class="number">0</span>,<span class="number">4</span>,<span class="number">1</span>),</span><br><span class="line">      <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Face3</span>(<span class="number">5</span>,<span class="number">2</span>,<span class="number">1</span>),</span><br><span class="line">      <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Face3</span>(<span class="number">5</span>,<span class="number">3</span>,<span class="number">2</span>),</span><br><span class="line">      <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Face3</span>(<span class="number">5</span>,<span class="number">4</span>,<span class="number">3</span>),</span><br><span class="line">      <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Face3</span>(<span class="number">5</span>,<span class="number">1</span>,<span class="number">4</span>),</span><br><span class="line">  ]</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 3.创建几何体架构</span></span><br><span class="line">  <span class="keyword">var</span> geom = <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">Geometry</span>()</span><br><span class="line">  geom.<span class="property">vertices</span> = vertices</span><br><span class="line">  geom.<span class="property">faces</span> = faces</span><br><span class="line">  geom.<span class="title function_">computeFaceNormals</span>()  <span class="comment">// 计算法向量, 这决定了对光做出的反应</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 4.添加材质</span></span><br><span class="line">  <span class="keyword">var</span> materials = [</span><br><span class="line">      <span class="comment">// 几何体面的材质 (Lambert材质适合光照)</span></span><br><span class="line">      <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">MeshLambertMaterial</span>(&#123; </span><br><span class="line">          <span class="attr">opacity</span>: <span class="number">0.8</span>, </span><br><span class="line">          <span class="attr">color</span>:<span class="number">0x44e144</span>, </span><br><span class="line">          <span class="attr">transparent</span>: <span class="literal">true</span> </span><br><span class="line">      &#125;),</span><br><span class="line">      <span class="comment">// 一个几何体框架的材质</span></span><br><span class="line">      <span class="keyword">new</span> <span class="variable constant_">THREE</span>.<span class="title class_">MeshBasicMaterial</span>(&#123;</span><br><span class="line">          <span class="attr">color</span>:<span class="number">0x008800</span>,</span><br><span class="line">          <span class="attr">wireframe</span>:<span class="literal">true</span></span><br><span class="line">      &#125;),</span><br><span class="line">  ]</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 5.创建几何体(传入结构, 材质)</span></span><br><span class="line">  mesh = <span class="variable constant_">THREE</span>.<span class="property">SceneUtils</span>.<span class="title function_">createMultiMaterialObject</span>(geom, materials) </span><br><span class="line">  mesh.<span class="property">name</span> = <span class="string">&#x27;源&#x27;</span></span><br><span class="line">  mesh.<span class="property">children</span>.<span class="title function_">forEach</span>(<span class="keyword">function</span> (<span class="params">e</span>) &#123;</span><br><span class="line">      e.<span class="property">castShadow</span> = <span class="literal">true</span>  <span class="comment">//可以生成影子</span></span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 6.添加进场景中</span></span><br><span class="line">  scene.<span class="title function_">add</span>(mesh);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h1 id="二、JS-安卓扫码"><a href="#二、JS-安卓扫码" class="headerlink" title="二、JS 安卓扫码"></a>二、JS 安卓扫码</h1><h2 id="Cordova-监听"><a href="#Cordova-监听" class="headerlink" title="Cordova 监听"></a>Cordova 监听</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> Native 安卓设备点击按钮触发扫描</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> 扫描枪：cordova-plugin-zebra-scanner、seuic、common</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> 摄像头：cordova plugin add phonegap-plugin-barcodescanner</span></span><br><span class="line"><span class="comment">**/</span></span><br><span class="line">$scope.$on(<span class="string">&#x27;$ionicView.afterEnter&#x27;</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    imanScanner.<span class="title function_">open</span>();</span><br><span class="line">&#125;);</span><br><span class="line">$scope.$on(<span class="string">&#x27;$ionicView.beforeLeave&#x27;</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    imanScanner.<span class="title function_">close</span>();</span><br><span class="line">&#125;);</span><br><span class="line">$scope.$on(<span class="string">&#x27;iScanner.DataReceived&#x27;</span>, <span class="keyword">function</span> (<span class="params">e, barcode</span>) &#123;</span><br><span class="line">  </span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">angular.<span class="title function_">module</span>(<span class="string">&#x27;umifNgBase&#x27;</span>)</span><br><span class="line">.<span class="title function_">factory</span>(<span class="string">&#x27;imanScanner&#x27;</span>, [<span class="string">&#x27;$ionicPlatform&#x27;</span>, <span class="string">&#x27;$rootScope&#x27;</span>, <span class="string">&#x27;imanUsbPort&#x27;</span>,</span><br><span class="line"><span class="keyword">function</span> (<span class="params">$ionicPlatform, $rootScope, imanUsbPort</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> _iscanner;</span><br><span class="line">    $ionicPlatform.<span class="title function_">ready</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">var</span> plugins = <span class="variable language_">window</span>.<span class="property">cordova</span> &amp;&amp; <span class="variable language_">window</span>.<span class="property">cordova</span>.<span class="property">plugins</span></span><br><span class="line">        <span class="keyword">if</span> (!plugins) &#123;</span><br><span class="line">            $rootScope.$on(<span class="string">&#x27;imanUsbPort.input&#x27;</span>, <span class="keyword">function</span> (<span class="params">e, data</span>) &#123;</span><br><span class="line">                $rootScope.$broadcast(<span class="string">&#x27;iScanner.DataReceived&#x27;</span>, data.<span class="title function_">trim</span>());</span><br><span class="line">            &#125;);</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (plugins.<span class="property">zebraScanner</span> &amp;&amp; plugins.<span class="property">zebraScanner</span>.<span class="property">valid</span>) &#123;</span><br><span class="line">            _iscanner = cordova.<span class="property">plugins</span>.<span class="property">zebraScanner</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (plugins.<span class="property">seuicScanner</span> &amp;&amp; plugins.<span class="property">seuicScanner</span>.<span class="property">valid</span>) &#123;</span><br><span class="line">            _iscanner = cordova.<span class="property">plugins</span>.<span class="property">seuicScanner</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (plugins.<span class="property">commonScanner</span> &amp;&amp; plugins.<span class="property">commonScanner</span>.<span class="property">valid</span>) &#123;</span><br><span class="line">            _iscanner = cordova.<span class="property">plugins</span>.<span class="property">commonScanner</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="variable language_">document</span>.<span class="title function_">addEventListener</span>(<span class="string">&#x27;Scanner.DataReceived&#x27;</span>, <span class="keyword">function</span> (<span class="params">e</span>) &#123;</span><br><span class="line">            $rootScope.$broadcast(<span class="string">&#x27;iScanner.DataReceived&#x27;</span>, e.<span class="property">scanData</span>.<span class="title function_">trim</span>());</span><br><span class="line">        &#125;, <span class="literal">false</span>);</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="keyword">return</span> &#123;<span class="attr">open</span>: openSanner, <span class="attr">close</span>: closeSanner, <span class="attr">cameraScan</span>: cameraScanner&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">openSanner</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (_iscanner) &#123;</span><br><span class="line">            _iscanner.<span class="title function_">addListener</span>();</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            imanUsbPort.<span class="title function_">start</span>();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">closeSanner</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (_iscanner) &#123;</span><br><span class="line">            _iscanner.<span class="title function_">removeListener</span>();</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            imanUsbPort.<span class="title function_">stop</span>();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">cameraScanner</span>(<span class="params"></span>) &#123;</span><br><span class="line">        cordova.<span class="property">plugins</span>.<span class="property">barcodeScanner</span>.<span class="title function_">scan</span>(</span><br><span class="line">        <span class="keyword">function</span> (<span class="params">res</span>) &#123;</span><br><span class="line">            $rootScope.$broadcast(<span class="string">&#x27;iScanner.DataReceived&#x27;</span>, res.<span class="property">text</span>.<span class="title function_">trim</span>());</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="keyword">function</span> (<span class="params">err</span>) &#123;</span><br><span class="line">            $rootScope.$broadcast(<span class="string">&#x27;iScanner.DataReceived&#x27;</span>, <span class="string">&quot;Fail：&quot;</span> + err);</span><br><span class="line">        &#125;, &#123;</span><br><span class="line">            <span class="attr">preferFrontCamera</span>: <span class="literal">false</span>,</span><br><span class="line">            <span class="attr">showFlipCameraButton</span>: <span class="literal">true</span>,</span><br><span class="line">            <span class="attr">showTorchButton</span>: <span class="literal">true</span>,</span><br><span class="line">            <span class="attr">torchOn</span>: <span class="literal">true</span>,</span><br><span class="line">            <span class="attr">saveHistory</span>: <span class="literal">true</span>,</span><br><span class="line">            <span class="attr">prompt</span>: <span class="string">&quot;Place a barcode inside the scan area&quot;</span>,</span><br><span class="line">            <span class="attr">resultDisplayDuration</span>: <span class="number">500</span>,</span><br><span class="line">            <span class="attr">formats</span>: <span class="string">&quot;QR_CODE,PDF_417&quot;</span>,</span><br><span class="line">            <span class="attr">orientation</span>: <span class="string">&quot;landscape&quot;</span>,</span><br><span class="line">            <span class="attr">disableAnimations</span>: <span class="literal">true</span>,</span><br><span class="line">            <span class="attr">disableSuccessBeep</span>: <span class="literal">false</span></span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;])</span><br></pre></td></tr></table></figure>

<h2 id="WebRTC-自动"><a href="#WebRTC-自动" class="headerlink" title="WebRTC 自动"></a>WebRTC 自动</h2><blockquote>
<p>基于 UDP 协议允许两个网页浏览器语音或视频的实时会话，直接建立浏览器之间点对点的连接而实现数据流的高效传输，区别 WebSocket：基于 TCP 协议允许浏览器和服务器通信，通过第三方服务器建立连接而有传输延迟。</p>
</blockquote>
  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> WebRTC API 实现自动扫描框内二维码</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> getUserMedia：打开摄像头或麦克风</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> canvas 捕捉摄像头的图像并实时绘制</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> jsQR   根据图像解析出二维码信息</span></span><br><span class="line"><span class="comment">**/</span></span><br><span class="line">navigator.<span class="property">mediaDevices</span>.<span class="title function_">getUserMedia</span>(&#123; </span><br><span class="line">    <span class="attr">video</span>: &#123;<span class="attr">width</span>: <span class="number">200</span>, <span class="attr">height</span>: <span class="number">200</span>, <span class="attr">facingMode</span>: &#123;<span class="attr">exact</span>: <span class="string">&#x27;environment&#x27;</span>&#125;&#125; </span><br><span class="line">&#125;).<span class="title function_">then</span>(<span class="keyword">function</span> (<span class="params">stream</span>) &#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">video</span> = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&#x27;video&#x27;</span>)</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">canvas</span> = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&#x27;canvas&#x27;</span>)</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">context</span> = <span class="variable language_">this</span>.<span class="property">canvas</span>.<span class="title function_">getContext</span>(<span class="string">&quot;2d&quot;</span>)</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">video</span>.<span class="property">srcObject</span> = stream;  </span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">video</span>.<span class="title function_">setAttribute</span>(<span class="string">&quot;playsinline&quot;</span>, <span class="literal">true</span>);  <span class="comment">// 阻止 IOS 视频全屏</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">video</span>.<span class="title function_">play</span>().<span class="title function_">then</span>(<span class="variable language_">this</span>.<span class="property">Scanning</span>);    <span class="comment">// 视频播放后开始周期性扫码</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="title class_">Scanning</span> () &#123;</span><br><span class="line">    <span class="title function_">requestAnimationFrame</span>(<span class="variable language_">this</span>.<span class="property">doScan</span>);</span><br><span class="line">&#125;,</span><br><span class="line">doScan () &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">video</span>.<span class="property">readyState</span> !== video.<span class="property">HAVE_ENOUGH_DATA</span>) &#123; <span class="keyword">return</span> &#125;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">context</span>.<span class="title function_">drawImage</span>(<span class="variable language_">this</span>.<span class="property">video</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">200</span>, <span class="number">200</span>);</span><br><span class="line">    <span class="keyword">let</span> imgData = <span class="variable language_">this</span>.<span class="property">context</span>.<span class="title function_">getImageData</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">200</span>, <span class="number">200</span>);</span><br><span class="line">    <span class="keyword">let</span> code = <span class="title function_">jsQR</span>(imgData.<span class="property">data</span>, imgData.<span class="property">width</span>, imgData.<span class="property">height</span>);</span><br><span class="line">    <span class="keyword">if</span> (code) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.$emit(<span class="string">&quot;scanned&quot;</span>, code);   <span class="comment">// 将结果传递给父组件</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">video</span>.<span class="property">srcObject</span>.<span class="title function_">getTracks</span>().<span class="title function_">forEach</span>(<span class="function"><span class="params">t</span> =&gt;</span> t.<span class="title function_">stop</span>());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h1 id="三、Video-视频"><a href="#三、Video-视频" class="headerlink" title="三、Video 视频"></a>三、Video 视频</h1><h2 id="组件封装"><a href="#组件封装" class="headerlink" title="组件封装"></a>组件封装</h2>  <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;video-controls&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">ref</span>=<span class="string">&quot;playControl&quot;</span> <span class="attr">:class</span>=<span class="string">&quot;&#123;</span></span></span><br><span class="line"><span class="string"><span class="tag">            &#x27;play-control&#x27;: true, </span></span></span><br><span class="line"><span class="string"><span class="tag">             playing: paused, </span></span></span><br><span class="line"><span class="string"><span class="tag">             paused: !paused&#125;&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">ref</span>=<span class="string">&quot;current&quot;</span> <span class="attr">class</span>=<span class="string">&quot;current-time&quot;</span>&gt;</span>&#123;&#123;currentTime&#125;&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">ref</span>=<span class="string">&quot;progress&quot;</span> <span class="attr">class</span>=<span class="string">&quot;progress&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;handleJumpProgress&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">i</span> <span class="attr">ref</span>=<span class="string">&quot;slider&quot;</span> <span class="attr">:style</span>=<span class="string">&quot;&#123;width: progress*100 + &#x27;%&#x27;&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">i</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;duration-time&quot;</span>&gt;</span>&#123;&#123;totalTime&#125;&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"><span class="keyword">import</span> <span class="title class_">Draggable</span> <span class="keyword">from</span> <span class="string">&quot;./draggable&quot;</span>;</span></span><br><span class="line"><span class="language-javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="language-javascript">    <span class="attr">name</span>: <span class="string">&quot;video-controls&quot;</span>,</span></span><br><span class="line"><span class="language-javascript">    <span class="attr">props</span>: [<span class="string">&quot;video&quot;</span>],</span></span><br><span class="line"><span class="language-javascript">    <span class="title function_">data</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="attr">paused</span>: <span class="literal">true</span>,</span></span><br><span class="line"><span class="language-javascript">            <span class="attr">currentTime</span>: <span class="string">&quot;00:00&quot;</span>,</span></span><br><span class="line"><span class="language-javascript">            <span class="attr">totalTime</span>: <span class="string">&quot;00:00&quot;</span>,</span></span><br><span class="line"><span class="language-javascript">            <span class="attr">progress</span>: <span class="number">0</span></span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">    &#125;,</span></span><br><span class="line"><span class="language-javascript">    <span class="attr">methods</span>: &#123;</span></span><br><span class="line"><span class="language-javascript">        handleJumpProgress,</span></span><br><span class="line"><span class="language-javascript">        handleDragProgress,</span></span><br><span class="line"><span class="language-javascript">        handleDurationChange,</span></span><br><span class="line"><span class="language-javascript">        handleTimeUpdate,</span></span><br><span class="line"><span class="language-javascript">        handleClickControl</span></span><br><span class="line"><span class="language-javascript">    &#125;,</span></span><br><span class="line"><span class="language-javascript">    <span class="title function_">mounted</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">this</span>.<span class="property">draggable</span> = <span class="title class_">Draggable</span>(<span class="variable language_">this</span>.<span class="property">$refs</span>.<span class="property">progress</span>, <span class="variable language_">this</span>.<span class="property">handleDragProgress</span>);</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">this</span>.<span class="property">video</span>.<span class="title function_">addEventListener</span>(<span class="string">&quot;durationchange&quot;</span>, <span class="variable language_">this</span>.<span class="property">handleDurationChange</span>);</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">this</span>.<span class="property">video</span>.<span class="title function_">addEventListener</span>(<span class="string">&quot;timeupdate&quot;</span>, <span class="variable language_">this</span>.<span class="property">handleTimeUpdate</span>);</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">this</span>.<span class="property">video</span>.<span class="title function_">addEventListener</span>(<span class="string">&quot;play&quot;</span>, <span class="function"><span class="params">e</span> =&gt;</span> (<span class="variable language_">this</span>.<span class="property">paused</span> = <span class="literal">false</span>));</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">this</span>.<span class="property">video</span>.<span class="title function_">addEventListener</span>(<span class="string">&quot;pause&quot;</span>, <span class="function"><span class="params">e</span> =&gt;</span> (<span class="variable language_">this</span>.<span class="property">paused</span> = <span class="literal">true</span>));</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">this</span>.<span class="property">$refs</span>.<span class="property">playControl</span>.<span class="title function_">addEventListener</span>(<span class="string">&quot;click&quot;</span>, <span class="variable language_">this</span>.<span class="property">handleClickControl</span>);</span></span><br><span class="line"><span class="language-javascript">    &#125;,</span></span><br><span class="line"><span class="language-javascript">    <span class="title function_">destroyed</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">this</span>.<span class="property">draggable</span>.<span class="title function_">destroy</span>();</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript">&#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"><span class="keyword">function</span> <span class="title function_">handleClickControl</span>(<span class="params">e</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">paused</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">this</span>.<span class="property">video</span>.<span class="title function_">play</span>();</span></span><br><span class="line"><span class="language-javascript">    &#125; <span class="keyword">else</span> &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">this</span>.<span class="property">video</span>.<span class="title function_">pause</span>();</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript">&#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"><span class="keyword">function</span> <span class="title function_">handleTimeUpdate</span>(<span class="params">e</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">this</span>.<span class="property">currentTime</span> = <span class="title function_">formatTime</span>(e.<span class="property">target</span>.<span class="property">currentTime</span>);</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">this</span>.<span class="property">progress</span> = e.<span class="property">target</span>.<span class="property">currentTime</span> / e.<span class="property">target</span>.<span class="property">duration</span>;</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">this</span>.<span class="property">paused</span> = <span class="literal">false</span>;</span></span><br><span class="line"><span class="language-javascript">&#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"><span class="keyword">function</span> <span class="title function_">handleDurationChange</span>(<span class="params">e</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">this</span>.<span class="property">totalTime</span> = <span class="title function_">formatTime</span>(e.<span class="property">target</span>.<span class="property">duration</span>);</span></span><br><span class="line"><span class="language-javascript">&#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"><span class="keyword">function</span> <span class="title function_">handleDragProgress</span>(<span class="params">delt</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">const</span> &#123; progress, slider &#125; = <span class="variable language_">this</span>.<span class="property">$refs</span>;</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">let</span> w = slider.<span class="property">offsetWidth</span> + delt;</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">if</span> (w &gt; progress.<span class="property">offsetWidth</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">      w = progress.<span class="property">offsetWidth</span>;</span></span><br><span class="line"><span class="language-javascript">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (w &lt; <span class="number">0</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">      w = <span class="number">0</span>;</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">this</span>.<span class="property">progress</span> = w / progress.<span class="property">offsetWidth</span>;</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">this</span>.<span class="property">video</span>.<span class="property">currentTime</span> = <span class="variable language_">this</span>.<span class="property">progress</span> * <span class="variable language_">this</span>.<span class="property">video</span>.<span class="property">duration</span>;</span></span><br><span class="line"><span class="language-javascript">&#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"><span class="keyword">function</span> <span class="title function_">handleJumpProgress</span>(<span class="params">e</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">const</span> target = e.<span class="property">currentTarget</span>;</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">const</span> rect = target.<span class="title function_">getBoundingClientRect</span>();</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">const</span> clickX = e.<span class="property">clientX</span> - rect.<span class="property">left</span>;</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">this</span>.<span class="property">progress</span> = clickX / rect.<span class="property">width</span>;</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">this</span>.<span class="property">video</span>.<span class="property">currentTime</span> = <span class="variable language_">this</span>.<span class="property">progress</span> * <span class="variable language_">this</span>.<span class="property">video</span>.<span class="property">duration</span>;</span></span><br><span class="line"><span class="language-javascript">&#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"><span class="keyword">function</span> <span class="title function_">formatTime</span>(<span class="params">seconds</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">const</span> m = <span class="title class_">Math</span>.<span class="title function_">floor</span>(seconds / <span class="number">60</span>);</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">const</span> s = <span class="title class_">Math</span>.<span class="title function_">ceil</span>(seconds % <span class="number">60</span>);</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">return</span> <span class="title function_">formatNumber</span>(m) + <span class="string">&quot;:&quot;</span> + <span class="title function_">formatNumber</span>(s);</span></span><br><span class="line"><span class="language-javascript">&#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"><span class="keyword">function</span> <span class="title function_">formatNumber</span>(<span class="params">num</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">return</span> num &lt; <span class="number">10</span> ? <span class="string">&quot;0&quot;</span> + num : num;</span></span><br><span class="line"><span class="language-javascript">&#125;</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">lang</span>=<span class="string">&quot;scss&quot;</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css"><span class="selector-class">.video-controls</span> &#123;</span></span><br><span class="line"><span class="language-css">    <span class="attribute">position</span>: absolute;</span></span><br><span class="line"><span class="language-css">    <span class="attribute">display</span>: flex;</span></span><br><span class="line"><span class="language-css">    <span class="attribute">height</span>: <span class="number">40px</span>;</span></span><br><span class="line"><span class="language-css">    <span class="attribute">left</span>: <span class="number">20px</span>;</span></span><br><span class="line"><span class="language-css">    <span class="attribute">right</span>: <span class="number">20px</span>;</span></span><br><span class="line"><span class="language-css">    <span class="attribute">bottom</span>: <span class="number">20px</span>;</span></span><br><span class="line"><span class="language-css">    <span class="attribute">background</span>: <span class="built_in">rgba</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0.5</span>);</span></span><br><span class="line"><span class="language-css">    <span class="attribute">border-radius</span>: <span class="number">2px</span>;</span></span><br><span class="line"><span class="language-css">    <span class="attribute">color</span>: <span class="number">#fff</span>;</span></span><br><span class="line"><span class="language-css">    <span class="attribute">align-items</span>: center;</span></span><br><span class="line"><span class="language-css">    <span class="attribute">text-align</span>: center;</span></span><br><span class="line"><span class="language-css">    <span class="attribute">font-size</span>: <span class="number">14px</span>;</span></span><br><span class="line"><span class="language-css">    <span class="attribute">line-height</span>: <span class="number">18px</span>;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.play-control</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">width</span>: <span class="number">40px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">padding-top</span>: <span class="number">1px</span>;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.playing</span><span class="selector-pseudo">::after</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">content</span>: <span class="string">&quot;&quot;</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">display</span>: inline-block;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">border-top</span>: <span class="number">8px</span> solid transparent;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">border-left</span>: <span class="number">12px</span> solid <span class="number">#fff</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">border-bottom</span>: <span class="number">8px</span> solid transparent;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">vertical-align</span>: top;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.paused</span><span class="selector-pseudo">::after</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">content</span>: <span class="string">&quot;&quot;</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">display</span>: inline-block;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">width</span>: <span class="number">8px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">height</span>: <span class="number">15px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">border-left</span>: <span class="number">2px</span> solid <span class="number">#fff</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">border-right</span>: <span class="number">2px</span> solid <span class="number">#fff</span>;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.progress</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">position</span>: relative;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">flex</span>: <span class="number">1</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">height</span>: <span class="number">100%</span>;</span></span><br><span class="line"><span class="language-css">        &amp;<span class="selector-pseudo">::before</span>, <span class="selector-tag">i</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">content</span>: <span class="string">&quot;&quot;</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">position</span>: absolute;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">100%</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">2px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">left</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">top</span>: <span class="number">50%</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin-top</span>: -<span class="number">1px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background</span>: <span class="number">#818a95</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">border-radius</span>: <span class="number">2px</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">i</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-color</span>: <span class="number">#fff</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">z-index</span>: <span class="number">2</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">i</span><span class="selector-pseudo">::after</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">content</span>: <span class="string">&quot;&quot;</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">position</span>: absolute;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">top</span>: -<span class="number">4px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">right</span>: -<span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">border-radius</span>: <span class="number">100%</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-color</span>: <span class="number">#fff</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.current-time</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">width</span>: <span class="number">50px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">text-align</span>: left;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-class">.duration-time</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">width</span>: <span class="number">60px</span>;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">&#125;</span></span><br><span class="line"><span class="language-css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// draggable.js</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">Draggable</span>(<span class="params">el, callback</span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> startX = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">let</span> isTouchStart = <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">    el.<span class="title function_">addEventListener</span>(<span class="string">&quot;touchstart&quot;</span>, handleDown);</span><br><span class="line">    <span class="variable language_">window</span>.<span class="title function_">addEventListener</span>(<span class="string">&quot;touchmove&quot;</span>, handleMove);</span><br><span class="line">    <span class="variable language_">window</span>.<span class="title function_">addEventListener</span>(<span class="string">&#x27;touchend&#x27;</span>, handleUp);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">handleDown</span>(<span class="params">e</span>) &#123;</span><br><span class="line">        isTouchStart = <span class="literal">true</span>;</span><br><span class="line">        startX = e.<span class="property">touches</span>[<span class="number">0</span>].<span class="property">pageX</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">handleMove</span>(<span class="params">e</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (!isTouchStart) &#123; <span class="keyword">return</span> &#125;</span><br><span class="line">        <span class="keyword">let</span> deltX = e.<span class="property">touches</span>[<span class="number">0</span>].<span class="property">pageX</span> - startX;</span><br><span class="line">        startX = e.<span class="property">touches</span>[<span class="number">0</span>].<span class="property">pageX</span>;</span><br><span class="line">        <span class="title function_">callback</span>(deltX);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">handleUp</span>(<span class="params"></span>) &#123;</span><br><span class="line">        isTouchStart = <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">destroy</span>(<span class="params"></span>) &#123;</span><br><span class="line">        el.<span class="title function_">removeEventListener</span>(<span class="string">&quot;touchstart&quot;</span>, handleDown);</span><br><span class="line">        <span class="variable language_">window</span>.<span class="title function_">removeEventListener</span>(<span class="string">&quot;touchmove&quot;</span>, handleMove);</span><br><span class="line">        <span class="variable language_">window</span>.<span class="title function_">removeEventListener</span>(<span class="string">&#x27;touchup&#x27;</span>, handleUp);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> &#123; destroy &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h2 id="组件使用"><a href="#组件使用" class="headerlink" title="组件使用"></a>组件使用</h2>  <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;video-player&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">video</span></span></span><br><span class="line"><span class="tag">            <span class="attr">ref</span>=<span class="string">&quot;video&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">autoplay</span></span></span><br><span class="line"><span class="tag">            <span class="attr">preload</span></span></span><br><span class="line"><span class="tag">            <span class="attr">disablePictureInPicture</span></span></span><br><span class="line"><span class="tag">            <span class="attr">controlslist</span>=<span class="string">&quot;nodownload nofullscreen&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">playsinline</span></span></span><br><span class="line"><span class="tag">            <span class="attr">webkit-playsinline</span></span></span><br><span class="line"><span class="tag">            <span class="attr">x5-video-player-type</span>=<span class="string">&quot;h5&quot;</span></span></span><br><span class="line"><span class="tag">            @<span class="attr">play</span>=<span class="string">&quot;isPaused = false&quot;</span></span></span><br><span class="line"><span class="tag">            @<span class="attr">pause</span>=<span class="string">&quot;isPaused = true&quot;</span></span></span><br><span class="line"><span class="tag">            @<span class="attr">contextmenu</span>=<span class="string">&quot;handleContext&quot;</span></span></span><br><span class="line"><span class="tag">            @<span class="attr">fullscreenchange</span>=<span class="string">&quot;handleFullScreenChange&quot;</span></span></span><br><span class="line"><span class="tag">        &gt;</span><span class="tag">&lt;/<span class="name">video</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">img</span> </span></span><br><span class="line"><span class="tag">            <span class="attr">width</span>=<span class="string">&quot;60&quot;</span> </span></span><br><span class="line"><span class="tag">            <span class="attr">src</span>=<span class="string">&quot;images/icon-play.png&quot;</span> </span></span><br><span class="line"><span class="tag">            @<span class="attr">click</span>=<span class="string">&quot;playVideo&quot;</span> </span></span><br><span class="line"><span class="tag">            <span class="attr">class</span>=<span class="string">&quot;&#123;&#x27;shown&#x27;: isPaused&#125;&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">video-control</span> <span class="attr">v-if</span>=<span class="string">&quot;video&quot;</span> <span class="attr">:video</span>=<span class="string">&quot;video&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"><span class="keyword">import</span> <span class="title class_">VideoControl</span> <span class="keyword">from</span> <span class="string">&quot;@/components/Controls.vue&quot;</span>;</span></span><br><span class="line"><span class="language-javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="language-javascript">    <span class="attr">name</span>: <span class="string">&quot;video-player&quot;</span>,</span></span><br><span class="line"><span class="language-javascript">    <span class="attr">components</span>: &#123; <span class="title class_">VideoControl</span> &#125;,</span></span><br><span class="line"><span class="language-javascript">    <span class="title function_">data</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="attr">video</span>: <span class="literal">null</span>,</span></span><br><span class="line"><span class="language-javascript">            <span class="attr">isPaused</span>: <span class="literal">true</span>,</span></span><br><span class="line"><span class="language-javascript">            <span class="attr">curIndex</span>: <span class="number">0</span>,</span></span><br><span class="line"><span class="language-javascript">            <span class="attr">videoList</span>: []</span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">    &#125;,</span></span><br><span class="line"><span class="language-javascript">    <span class="attr">methods</span>: &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="title function_">playVideo</span>(<span class="params">e</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">let</span> video = <span class="variable language_">this</span>.<span class="property">$refs</span>.<span class="property">video</span>;</span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">if</span> (video.<span class="property">paused</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                video.<span class="title function_">play</span>();</span></span><br><span class="line"><span class="language-javascript">            &#125; <span class="keyword">else</span> &#123;</span></span><br><span class="line"><span class="language-javascript">                video.<span class="title function_">pause</span>();</span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript">        &#125;,</span></span><br><span class="line"><span class="language-javascript">        <span class="title function_">handleContext</span>(<span class="params">e</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">            e.<span class="title function_">preventDefault</span>();</span></span><br><span class="line"><span class="language-javascript">        &#125;,</span></span><br><span class="line"><span class="language-javascript">        onTap,</span></span><br><span class="line"><span class="language-javascript">        handleFullScreenChange,</span></span><br><span class="line"><span class="language-javascript">    &#125;,</span></span><br><span class="line"><span class="language-javascript">    <span class="title function_">created</span>(<span class="params"></span>)&#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">this</span>.<span class="title function_">getVideoUrl</span>(<span class="number">0</span>)</span></span><br><span class="line"><span class="language-javascript">    &#125;,</span></span><br><span class="line"><span class="language-javascript">    <span class="title function_">mounted</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">this</span>.<span class="title function_">onTap</span>(<span class="variable language_">this</span>.<span class="property">$refs</span>.<span class="property">video</span>, <span class="variable language_">this</span>.<span class="property">playVideo</span>);</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">this</span>.<span class="property">video</span> = <span class="variable language_">this</span>.<span class="property">$refs</span>.<span class="property">video</span>;</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">this</span>.<span class="property">video</span>.<span class="title function_">addEventListener</span>(<span class="string">&quot;ended&quot;</span>, <span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">curIndex</span> &lt; <span class="variable language_">this</span>.<span class="property">videoList</span>.<span class="property">length</span> - <span class="number">1</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="variable language_">this</span>.<span class="property">curIndex</span>++;</span></span><br><span class="line"><span class="language-javascript">                <span class="variable language_">this</span>.<span class="property">$refs</span>.<span class="property">video</span>.<span class="property">src</span> = res.<span class="property">value</span>;</span></span><br><span class="line"><span class="language-javascript">                <span class="variable language_">this</span>.<span class="property">$refs</span>.<span class="property">video</span>.<span class="title function_">play</span>();</span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript">        &#125;);</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript">&#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"><span class="keyword">function</span> <span class="title function_">onTap</span>(<span class="params">el, callback</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">let</span> touchStarted = <span class="literal">false</span>;</span></span><br><span class="line"><span class="language-javascript">    el.<span class="title function_">addEventListener</span>(<span class="string">&#x27;touchstart&#x27;</span>, <span class="keyword">function</span> (<span class="params">e</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        touchStarted = <span class="literal">true</span>;</span></span><br><span class="line"><span class="language-javascript">        <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="language-javascript">            touchStarted = <span class="literal">false</span></span></span><br><span class="line"><span class="language-javascript">        &#125;, <span class="number">500</span>)</span></span><br><span class="line"><span class="language-javascript">    &#125;)</span></span><br><span class="line"><span class="language-javascript">    el.<span class="title function_">addEventListener</span>(<span class="string">&#x27;touchmove&#x27;</span>, <span class="keyword">function</span> (<span class="params">e</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        touchStarted = <span class="literal">false</span></span></span><br><span class="line"><span class="language-javascript">    &#125;)</span></span><br><span class="line"><span class="language-javascript">    el.<span class="title function_">addEventListener</span>(<span class="string">&#x27;touchend&#x27;</span>, <span class="keyword">function</span> (<span class="params">e</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">if</span> (touchStarted) &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="title function_">callback</span>(e)</span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">    &#125;)</span></span><br><span class="line"><span class="language-javascript">&#125;</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">lang</span>=<span class="string">&quot;scss&quot;</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css"><span class="selector-class">.video-player</span> &#123;</span></span><br><span class="line"><span class="language-css">    <span class="attribute">position</span>: relative;</span></span><br><span class="line"><span class="language-css">    <span class="selector-tag">video</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">display</span>: block;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">width</span>: <span class="number">100vw</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">height</span>: <span class="number">100vh</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">background-color</span>: <span class="number">#243c70</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">object-fit</span>: fill;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    <span class="selector-tag">img</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">position</span>: absolute;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">left</span>: <span class="number">50%</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">top</span>: <span class="number">50%</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">transform</span>: <span class="built_in">translate</span>(-<span class="number">50%</span>, -<span class="number">50%</span>);</span></span><br><span class="line"><span class="language-css">        <span class="attribute">opacity</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">        &amp;<span class="selector-class">.shown</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">opacity</span>: <span class="number">1</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">&#125;</span></span><br><span class="line"><span class="language-css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>


<h1 id="四、Agora-声网"><a href="#四、Agora-声网" class="headerlink" title="四、Agora 声网"></a>四、Agora 声网</h1><blockquote>
<p>用于为浏览器提供实时通信的 JS 接口，运用场景有：语音通话（一对一、多对多）、视频通话（一对一、多对多）、互动直播（语音、视频）、互动游戏（游戏内置实时语音、视频通话）、录制（服务端录制）、信令（呼叫、消息传递、状态同步等）。</p>
</blockquote>
<h2 id="RTM-实时消息"><a href="#RTM-实时消息" class="headerlink" title="RTM 实时消息"></a>RTM 实时消息</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">ArRTM</span> <span class="keyword">from</span> <span class="string">&quot;ar-rtm-sdk&quot;</span></span><br><span class="line"><span class="keyword">var</span> rtmClient = <span class="title class_">ArRTM</span>.<span class="title function_">createInstance</span>(<span class="title class_">App</span> <span class="variable constant_">ID</span> );</span><br><span class="line">rtmClient.<span class="title function_">login</span>(&#123;<span class="attr">uid</span>: 用户id&#125;)； <span class="comment">// 用户 id 唯一，登录成功后才可以使用 RTM</span></span><br><span class="line"><span class="comment">// rtmClient.logout();          //  退出登录</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// ======================= 被邀请用户(接受者) ========================</span></span><br><span class="line"><span class="comment">// 收到主叫邀请用户</span></span><br><span class="line">rtmClient.<span class="title function_">on</span>(<span class="string">&quot;RemoteInvitationReceived&quot;</span>, <span class="keyword">function</span> (<span class="params">remoteInvitation</span>) &#123;</span><br><span class="line">  <span class="comment">// 返回给被叫：接受呼叫邀请成功。</span></span><br><span class="line">  remoteInvitation.<span class="title function_">on</span>(<span class="string">&quot;RemoteInvitationAccepted&quot;</span>, <span class="keyword">async</span> <span class="keyword">function</span> (<span class="params">res</span>) &#123;</span><br><span class="line">      <span class="comment">// 获取传过来的频道房间</span></span><br><span class="line">      <span class="keyword">var</span> invitationResponse = <span class="title class_">JSON</span>.<span class="title function_">parse</span>(res);</span><br><span class="line">      <span class="comment">// 加入实时通讯频道</span></span><br><span class="line">      <span class="keyword">await</span> rtcClient.<span class="title function_">join</span>(<span class="title class_">App</span> <span class="variable constant_">ID</span>, invitationContent.<span class="property">ChanId</span>, <span class="literal">null</span>, <span class="literal">null</span>)</span><br><span class="line">      <span class="comment">// 采集并发布媒体流</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 返回给被叫：拒绝呼叫邀请成功。</span></span><br><span class="line">  remoteInvitation.<span class="title function_">on</span>(<span class="string">&quot;RemoteInvitationRefused&quot;</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;&#125;);</span><br><span class="line">  <span class="comment">// 返回给被叫：主叫已取消呼叫邀请。</span></span><br><span class="line">  remoteInvitation.<span class="title function_">on</span>(<span class="string">&quot;RemoteInvitationCanceled&quot;</span>, <span class="keyword">function</span> (<span class="params">content</span>) &#123;&#125;);</span><br><span class="line">  <span class="comment">// 返回给被叫：呼叫邀请进程失败。</span></span><br><span class="line">  remoteInvitation.<span class="title function_">on</span>(<span class="string">&quot;RemoteInvitationFailure&quot;</span>, <span class="keyword">function</span> (<span class="params">reason</span>) &#123;&#125;);</span><br><span class="line">    </span><br><span class="line">  <span class="comment">// 接受邀请</span></span><br><span class="line">  remoteInvitation.<span class="title function_">accept</span>();   <span class="comment">//拒绝邀请</span></span><br><span class="line">  <span class="comment">// remoteInvitation.refuse();</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// ========================= 主叫邀请(发起者) ==========================</span></span><br><span class="line">...</span><br><span class="line"><span class="comment">// 查询被邀请用户的状态 被邀请用户id calleeId 唯一</span></span><br><span class="line"><span class="keyword">var</span> userOnlineRes = <span class="keyword">await</span> rtmClient.<span class="title function_">queryPeersOnlineStatus</span>([calleeId]);</span><br><span class="line"><span class="keyword">if</span> (!userOnlineRes[calleeId] || !userOnlineRes[calleeId]) &#123;</span><br><span class="line">  <span class="comment">// 不允许呼叫，因为对方不在线</span></span><br><span class="line">  <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建呼叫邀请</span></span><br><span class="line"><span class="keyword">var</span> localInvitation = rtmClient.<span class="title function_">createLocalInvitation</span>(calleeId);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 将呼叫邀请的内容 设置为视频通讯时使用的频道id - 进入同一个频道</span></span><br><span class="line">localInvitation.<span class="property">content</span> = <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(&#123;</span><br><span class="line">    <span class="title class_">ChanId</span>: <span class="string">&quot;4562&quot;</span> , <span class="comment">//符合命名规则即可</span></span><br><span class="line">&#125;); </span><br><span class="line"></span><br><span class="line"><span class="comment">// 发送呼叫邀请</span></span><br><span class="line">localInvitation.<span class="title function_">send</span>();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 返回给主叫：被叫已收到呼叫邀请。</span></span><br><span class="line">localInvitation.<span class="title function_">on</span>(<span class="string">&quot;LocalInvitationReceivedByPeer&quot;</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 对方收到邀请，说明对方已经上线，这个时候应该监听对方的在线状态</span></span><br><span class="line">    <span class="comment">// 如果对方离线 主动取消邀请（防止对方刷新或掉线时无法通知服务端）</span></span><br><span class="line">    rtmClient.<span class="title function_">subscribePeersOnlineStatus</span>([localInvitation.<span class="property">calleeId</span>]);</span><br><span class="line">    rtmClient.<span class="title function_">on</span>(<span class="string">&quot;PeersOnlineStatusChanged&quot;</span>, <span class="function">(<span class="params">userOnlineStatus</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (userOnlineStatus[localInvitation.<span class="property">calleeId</span>] === <span class="string">&quot;OFFLINE&quot;</span> &amp;&amp; </span><br><span class="line">        <span class="title class_">Store</span>.<span class="property">iscalling</span>) &#123;</span><br><span class="line">            localInvitation.<span class="title function_">cancel</span>();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// 返回给主叫：被叫已接受呼叫邀请。</span></span><br><span class="line">localInvitation.<span class="title function_">on</span>(<span class="string">&quot;LocalInvitationAccepted&quot;</span>, <span class="keyword">async</span> <span class="keyword">function</span> (<span class="params">res</span>) &#123;</span><br><span class="line">    <span class="comment">// 获取传过来的频道房间</span></span><br><span class="line">    <span class="keyword">var</span> invitationResponse = <span class="title class_">JSON</span>.<span class="title function_">parse</span>(res);	      <span class="comment">// 加入实时通讯频道</span></span><br><span class="line">    <span class="keyword">await</span> rtcClient.<span class="title function_">join</span>(<span class="title class_">App</span> <span class="variable constant_">ID</span>, invitationContent.<span class="property">ChanId</span>, <span class="literal">null</span>, <span class="literal">null</span>)</span><br><span class="line">    <span class="comment">// 采集并发布媒体流</span></span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// 远端用户拒绝了你的呼叫邀请</span></span><br><span class="line">localInvitation.<span class="title function_">on</span>(<span class="string">&quot;LocalInvitationRefused&quot;</span>, <span class="keyword">function</span> (<span class="params">res</span>) &#123;&#125;);</span><br><span class="line"><span class="comment">// 返回给主叫：呼叫邀请已被成功取消。</span></span><br><span class="line">localInvitation.<span class="title function_">on</span>(<span class="string">&quot;LocalInvitationCanceled&quot;</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;&#125;);</span><br><span class="line"><span class="comment">// 返回给主叫：呼叫邀请进程失败。</span></span><br><span class="line">localInvitation.<span class="title function_">on</span>(<span class="string">&quot;LocalInvitationFailure&quot;</span>, <span class="keyword">function</span> (<span class="params">reason</span>) &#123;&#125;);</span><br><span class="line">...</span><br><span class="line"><span class="comment">// 发起者取消呼叫</span></span><br><span class="line"><span class="comment">// localInvitation.cancel();</span></span><br><span class="line">...</span><br></pre></td></tr></table></figure>

<h2 id="RTC-实时音视频"><a href="#RTC-实时音视频" class="headerlink" title="RTC 实时音视频"></a>RTC 实时音视频</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建 rtc 实例：编码格式、使用场景</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">ArRTC</span> <span class="keyword">from</span> <span class="string">&quot;ar-rtc-sdk&quot;</span></span><br><span class="line"><span class="keyword">var</span> rtcClient = <span class="title class_">ArRTC</span>.<span class="title function_">createClient</span>(&#123; <span class="attr">mode</span>: <span class="string">&quot;vp8&quot;</span>, <span class="attr">codec</span>: rtc&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 采集视频、音频 并进行预览、发布</span></span><br><span class="line">......</span><br><span class="line"><span class="comment">// 采集视频、音频设备</span></span><br><span class="line"><span class="keyword">var</span> [cameras, microhones] = <span class="keyword">await</span> <span class="title class_">Promise</span>.<span class="title function_">all</span>([</span><br><span class="line">        <span class="title class_">ArRTC</span>.<span class="title function_">getCameras</span>(),</span><br><span class="line">        <span class="title class_">ArRTC</span>.<span class="title function_">getMicrophones</span>(),</span><br><span class="line">    ]);</span><br><span class="line"><span class="keyword">if</span> (cameras.<span class="property">length</span> === <span class="number">0</span> &amp;&amp; microhones.<span class="property">length</span> === <span class="number">0</span>) &#123;</span><br><span class="line">    <span class="title function_">alert</span>(<span class="string">&quot;上麦失败！确实麦克风和摄像头&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">if</span> (cameras.<span class="property">length</span> &gt; <span class="number">0</span> &amp;&amp; microhones.<span class="property">length</span> &gt; <span class="number">0</span>) &#123; </span><br><span class="line">    <span class="comment">// 创建音频、视频轨道</span></span><br><span class="line">    <span class="keyword">let</span> <span class="title class_">TrackData</span> = <span class="keyword">await</span> <span class="title class_">ArRTC</span>.<span class="title function_">createMicrophoneAndCameraTracks</span>(<span class="literal">null</span>, &#123; </span><br><span class="line">        <span class="attr">encoderConfig</span>: &#123; </span><br><span class="line">            <span class="attr">bitrateMax</span>: <span class="number">1130</span>, </span><br><span class="line">            <span class="attr">frameRate</span>: <span class="number">15</span>, </span><br><span class="line">            <span class="attr">height</span>: <span class="number">180</span>, </span><br><span class="line">            <span class="attr">width</span>: <span class="number">320</span> </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    [audioTrack, videoTrack] = <span class="title class_">TrackData</span></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (!videoTrack &amp;&amp; !audioTrack) &#123;</span><br><span class="line">        <span class="title function_">alert</span>(<span class="string">&quot;没有设备无法发布媒体流&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 预览本地图像 documentId 存放容器的id（html中的属性id)</span></span><br><span class="line">videoTrack &amp;&amp; videoTrack.<span class="title function_">play</span>(documentId);</span><br><span class="line"><span class="comment">// 把用户设置为主播角色</span></span><br><span class="line">rtcClient.<span class="title function_">setClientRole</span>(<span class="string">&quot;host&quot;</span>);</span><br><span class="line"><span class="comment">// 发布</span></span><br><span class="line">rtcClient.<span class="title function_">publish</span>([videoTrack, audioTrack])</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 监听相关回调</span></span><br><span class="line"><span class="comment">// 通知远端用户发布了新的音频或者视频</span></span><br><span class="line">rtcClient.<span class="title function_">on</span>(<span class="string">&quot;user-published&quot;</span>, <span class="keyword">async</span> <span class="keyword">function</span> (<span class="params">user, mediaType</span>) &#123;</span><br><span class="line">    <span class="comment">// 订阅发布的音频或者视频</span></span><br><span class="line">    <span class="keyword">await</span> rtcClient.<span class="title function_">subscribe</span>(user, mediaType);	</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// 通知远端用户取消发布了音频或视频</span></span><br><span class="line">rtcClient.<span class="title function_">on</span>(<span class="string">&quot;user-unpublished&quot;</span>, <span class="keyword">async</span> <span class="keyword">function</span> (<span class="params">user, mediaType</span>) &#123;&#125;);</span><br><span class="line"><span class="comment">// 远端用户加入频道</span></span><br><span class="line">rtcClient.<span class="title function_">on</span>(<span class="string">&quot;user-joined&quot;</span>, <span class="keyword">function</span> (<span class="params">user</span>) &#123;&#125;);</span><br><span class="line"><span class="comment">// 远端用户离线</span></span><br><span class="line">rtcClient.<span class="title function_">on</span>(<span class="string">&quot;user-left&quot;</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">  <span class="comment">// 释放资源				</span></span><br><span class="line">  videoTrack &amp;&amp; (videoTrack.<span class="title function_">close</span>(), videoTrack = <span class="literal">null</span>);</span><br><span class="line">  audioTrack &amp;&amp; (audioTrack.<span class="title function_">close</span>(), audioTrack = <span class="literal">null</span>);</span><br><span class="line">  rtcClient.<span class="title function_">leave</span>();  <span class="comment">// 离开</span></span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// SDK 与服务器的连接状态发生改变回调</span></span><br><span class="line">rtcClient.<span class="title function_">on</span>(<span class="string">&quot;connection-state-change&quot;</span>,<span class="keyword">async</span> <span class="keyword">function</span>(<span class="params">ConnectionState</span>)&#123;&#125;);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 视频开关</span></span><br><span class="line">videoTrack.<span class="property">isMuted</span> = !videoTrack.<span class="property">isMuted</span>;</span><br><span class="line">videoTrack.<span class="title function_">setEnabled</span>(!videoTrack.<span class="property">isMuted</span>);</span><br><span class="line"><span class="comment">// 音频开关</span></span><br><span class="line">audioTrack.<span class="property">isMuted</span> = !audioTrack.<span class="property">isMuted</span>;</span><br><span class="line">audioTrack.<span class="title function_">setEnabled</span>(!audioTrack.<span class="property">isMuted</span>);</span><br></pre></td></tr></table></figure>


<h2 id="项目集成"><a href="#项目集成" class="headerlink" title="项目集成"></a>项目集成</h2>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> web 集成</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> rtc.js：创建本地的音视频流并监听远端的音视频流</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> AgoraRTC：软件包安装命令为 npm i agora-rtc-sdk -S</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> UID：同一个频道不能重复，PC 使用当年*2+随机数，小程序使用当年+随机数</span></span><br><span class="line"><span class="comment">**/</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">AgoraRTC</span> <span class="keyword">from</span> <span class="string">&#x27;agora-rtc-sdk&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> appId = <span class="string">&#x27;cf95ca211f164383b833f2e9bcdae701&#x27;</span>;</span><br><span class="line"><span class="keyword">let</span> videoContainerId = <span class="string">&#x27;control_panel_remote_video&#x27;</span>;</span><br><span class="line"><span class="keyword">let</span> localVideoContainerId = <span class="string">&#x27;control_panel_local_video&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">createRtcClient</span>(<span class="params">&#123;channel, UID, token&#125;, onAfterJoin</span>) &#123;</span><br><span class="line">  <span class="keyword">let</span> client = <span class="title class_">AgoraRTC</span>.<span class="title function_">createClient</span>(&#123; </span><br><span class="line">      <span class="comment">// live：直播模式，用户分为主播和观众。rtc：通信模式，任何用户自由说话</span></span><br><span class="line">      <span class="attr">mode</span>: <span class="string">&#x27;live&#x27;</span>, </span><br><span class="line">      <span class="comment">// 编解码可选 h264、vp8。与小程序链接时则必须使用 h264</span></span><br><span class="line">      <span class="attr">codec</span>: <span class="string">&quot;h264&quot;</span>  </span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  <span class="keyword">let</span> localStream;</span><br><span class="line">  <span class="keyword">let</span> audioMuted = <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">  <span class="title class_">AgoraRTC</span>.<span class="property">Logger</span>.<span class="title function_">setLogLevel</span>(-<span class="number">1</span>);</span><br><span class="line">  <span class="title class_">AgoraRTC</span>.<span class="property">Logger</span>.<span class="title function_">enableLogUpload</span>();</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 初始化 Client 对象 </span></span><br><span class="line">  client.<span class="title function_">init</span>(appId, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    client.<span class="title function_">join</span>(token, channel, <span class="variable constant_">UID</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line"></span><br><span class="line">      <span class="comment">// 创建并初始化本地的音视频流</span></span><br><span class="line">      localStream = <span class="title class_">AgoraRTC</span>.<span class="title function_">createStream</span>(&#123; </span><br><span class="line">        <span class="attr">streamID</span>: <span class="variable constant_">UID</span>, </span><br><span class="line">        <span class="attr">audio</span>: <span class="literal">true</span>, </span><br><span class="line">        <span class="attr">video</span>: <span class="literal">true</span>, </span><br><span class="line">        <span class="attr">screen</span>: <span class="literal">false</span> </span><br><span class="line">      &#125;);</span><br><span class="line">      localStream.<span class="title function_">setVideoProfile</span>(<span class="string">&#x27;360p_3&#x27;</span>);</span><br><span class="line">      localStream.<span class="title function_">setAudioProfile</span>(<span class="string">&#x27;speech_standard&#x27;</span>);</span><br><span class="line"></span><br><span class="line">      localStream.<span class="title function_">init</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">        client.<span class="title function_">publish</span>(localStream, <span class="keyword">function</span> (<span class="params">err</span>) &#123;</span><br><span class="line">          <span class="variable language_">console</span>.<span class="title function_">error</span>(<span class="string">&quot;Publish local stream error: &quot;</span>, err)</span><br><span class="line">        &#125;);</span><br><span class="line">        localStream.<span class="title function_">play</span>(localVideoContainerId)</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 监听远端的音视频流</span></span><br><span class="line">  client.<span class="title function_">on</span>(<span class="string">&#x27;stream-added&#x27;</span>, <span class="keyword">function</span> (<span class="params">evt</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> stream = evt.<span class="property">stream</span>;</span><br><span class="line">    <span class="keyword">var</span> uid = stream.<span class="title function_">getId</span>();</span><br><span class="line"></span><br><span class="line">    client.<span class="title function_">subscribe</span>(stream);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span> (onAfterJoin &amp;&amp; !client.<span class="property">clientUID</span>) &#123;</span><br><span class="line">      <span class="comment">// 视频录制：须放到客户进入频道拿到 uid 之后触发</span></span><br><span class="line">      client.<span class="property">clientUID</span> = uid;</span><br><span class="line">      <span class="title function_">onAfterJoin</span>(uid)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  client.<span class="title function_">on</span>(<span class="string">&#x27;stream-subscribed&#x27;</span>, <span class="keyword">function</span>(<span class="params">evt</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> remoteStream = evt.<span class="property">stream</span>;</span><br><span class="line">    remoteStream.<span class="title function_">play</span>(videoContainerId, &#123; <span class="attr">fit</span>: <span class="string">&#x27;contain&#x27;</span> &#125;, <span class="keyword">function</span> (<span class="params">err</span>)&#123;</span><br><span class="line">      <span class="keyword">if</span> (err.<span class="property">status</span> !== <span class="string">&quot;aborted&quot;</span>) &#123;</span><br><span class="line">        <span class="title class_">Notification</span>.<span class="title function_">warning</span>(&#123;</span><br><span class="line">          <span class="attr">title</span>: <span class="string">&#x27;提示&#x27;</span>,</span><br><span class="line">          <span class="attr">message</span>: <span class="string">&#x27;远端视频播放失败，点此重试&#x27;</span>,</span><br><span class="line">          <span class="title function_">onClick</span>(<span class="params"></span>) &#123;</span><br><span class="line">            remoteStream.<span class="title function_">resume</span>()</span><br><span class="line">              .<span class="title function_">then</span>(<span class="keyword">function</span> (<span class="params">result</span>) &#123;</span><br><span class="line">                <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;resume-remote-success, 恢复成功：&#x27;</span> + result)</span><br><span class="line">                notify.<span class="title function_">close</span>();</span><br><span class="line">              &#125;)</span><br><span class="line">              .<span class="title function_">catch</span>(<span class="keyword">function</span> (<span class="params">reason</span>) &#123;</span><br><span class="line">                <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;resume-remote-fail, 恢复失败：&#x27;</span> + reason)</span><br><span class="line">              &#125;)</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">    </span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  client.<span class="title function_">on</span>(<span class="string">&#x27;stream-removed&#x27;</span>, <span class="keyword">function</span> (<span class="params">evt</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> stream = evt.<span class="property">stream</span>;</span><br><span class="line">    stream.<span class="title function_">stop</span>();</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;Remote stream is removed &quot;</span> + stream.<span class="title function_">getId</span>());</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 用户主动挂断，client.destroy 触发而离开频道，不触发 peer-leave</span></span><br><span class="line">  client.<span class="title function_">on</span>(<span class="string">&quot;peer-leave&quot;</span>, <span class="keyword">function</span> (<span class="params">evt</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> uid = evt.<span class="property">uid</span>;</span><br><span class="line">    <span class="title function_">writeLog</span>(<span class="string">&quot;remote left user&quot;</span>, uid);</span><br><span class="line">    <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(videoContainerId).<span class="property">innerHTML</span> = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  client.<span class="property">destroy</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    client.<span class="title function_">leave</span>();</span><br><span class="line">    <span class="keyword">if</span> (localStream) &#123;</span><br><span class="line">      client.<span class="title function_">unpublish</span>(localStream);</span><br><span class="line">      localStream.<span class="title function_">stop</span>();</span><br><span class="line">      localStream.<span class="title function_">close</span>();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> lcontainer = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(localVideoContainerId);</span><br><span class="line">    <span class="keyword">if</span> (lcontainer) &#123;</span><br><span class="line">      lcontainer.<span class="property">innerHTML</span> = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    client = <span class="literal">null</span>;</span><br><span class="line">    localStream = <span class="literal">null</span>;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;rtc client destroyed!&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  client.<span class="property">toggleAudio</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (audioMuted) &#123;</span><br><span class="line">      <span class="keyword">if</span> (localStream.<span class="title function_">unmuteAudio</span>()) &#123;</span><br><span class="line">        audioMuted = <span class="literal">false</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="keyword">if</span> (localStream.<span class="title function_">muteAudio</span>()) &#123;</span><br><span class="line">        audioMuted = <span class="literal">true</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> audioMuted;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  client.<span class="property">muteVideo</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    localStream.<span class="title function_">muteVideo</span>();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> client;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span> store.js</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> roomNo：上线时获取的房间号，下线则清除</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> UID：同一个频道不能重复，PC 使用当年*2+随机数，小程序使用当年+随机数</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> rtcClientTickTimer：设定通话时间超过30分钟则提示，无操作则自动结束</span></span><br><span class="line"><span class="comment">**/</span></span><br><span class="line"><span class="keyword">const</span> <span class="variable constant_">UID</span> = <span class="built_in">parseInt</span>(<span class="keyword">new</span> <span class="title class_">Date</span>().<span class="title function_">getFullYear</span>() * <span class="number">2</span> + <span class="string">&#x27;&#x27;</span> + </span><br><span class="line"><span class="built_in">parseInt</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * <span class="number">100000</span>));</span><br><span class="line"><span class="keyword">let</span> currentRtcClient = <span class="literal">null</span>;</span><br><span class="line"><span class="keyword">let</span> rtcClientTickTimer = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">doCreateRtcClient</span>(<span class="params">roomNo, reconnect</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span>(curRtcClient) &#123; <span class="keyword">return</span> &#125;</span><br><span class="line">  </span><br><span class="line">    videoService.<span class="title function_">getAgoraToken</span>(&#123; <span class="attr">uid</span>: <span class="variable constant_">UID</span>, roomNo &#125;)</span><br><span class="line">    .<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (res.<span class="property">success</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> res.<span class="property">value</span></span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="title class_">Promise</span>.<span class="title function_">reject</span>(res)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">    .<span class="title function_">then</span>(<span class="function"><span class="params">token</span> =&gt;</span> &#123;</span><br><span class="line">        curRtcClient = <span class="title function_">createRtcClient</span>(&#123; </span><br><span class="line">            <span class="attr">channel</span>: roomNo, <span class="variable constant_">UID</span>, token </span><br><span class="line">        &#125;,  <span class="function"><span class="params">uid</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (reconnect !== <span class="literal">true</span>) &#123;</span><br><span class="line">                <span class="comment">// 开始视频录制</span></span><br><span class="line">                videoService.<span class="title function_">beginRecord</span>(roomNo, uid).<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">                    <span class="keyword">if</span> (res.<span class="property">success</span>) &#123; <span class="keyword">return</span> &#125;</span><br><span class="line">                    <span class="title function_">notify</span>(<span class="string">&#x27;启用视频录制失败（不影响报案）&#x27;</span>, <span class="string">&#x27;warning&#x27;</span>)</span><br><span class="line">                &#125;)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="title function_">longComunicationConfirm</span>();</span><br><span class="line">    &#125;).<span class="title function_">catch</span>(<span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="title function_">notify</span>(<span class="string">&#x27;建立通话失败&#x27;</span>, <span class="string">&#x27;warning&#x27;</span>);</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">error</span>(err)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">longComunicationConfirm</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">window</span>.<span class="property">location</span>.<span class="property">hostname</span> !== <span class="string">&#x27;video-claim.zhongan.io&#x27;</span>) &#123;</span><br><span class="line">        rtcClientTickTimer = <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">            <span class="title function_">showConfirm</span>(<span class="string">&#x27;通话时间过长，为避免通话时间浪费，无操作30s后自动退出！&#x27;</span>, &#123;</span><br><span class="line">                <span class="attr">confirmButtonText</span>: <span class="string">&#x27;继续通话&#x27;</span>,</span><br><span class="line">                <span class="attr">cancelButtonText</span>: <span class="string">&#x27;结束通话&#x27;</span>,</span><br><span class="line">                <span class="attr">showClose</span>: <span class="literal">false</span>,</span><br><span class="line">                <span class="attr">showCancelButton</span>: <span class="literal">false</span></span><br><span class="line">            &#125;).<span class="title function_">then</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">              <span class="built_in">clearTimeout</span>(rtcClientTickTimer);</span><br><span class="line">                <span class="comment">// 如果已经主动挂断，则无需再次提示</span></span><br><span class="line">                <span class="keyword">if</span> (rtcClientTickTimer &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                    <span class="title function_">longComunicationConfirm</span>();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;)</span><br><span class="line">            rtcClientTickTimer = <span class="built_in">setTimeout</span>(destroyRtcClient, <span class="number">30</span> * <span class="number">1000</span>)</span><br><span class="line">        &#125;, <span class="number">30</span> * <span class="number">60</span> * <span class="number">1000</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">destroyRtcClient</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (curRtcClient) &#123;</span><br><span class="line">        curRtcClient.<span class="title function_">destroy</span>();</span><br><span class="line">        curRtcClient = <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    ping.<span class="title function_">stop</span>();</span><br><span class="line">    logger.<span class="title function_">send</span>(&#123; <span class="attr">type</span>: <span class="string">&#x27;destroyRtcClient&#x27;</span> &#125;);</span><br><span class="line">    <span class="built_in">clearTimeout</span>(rtcClientTickTimer);</span><br><span class="line">    rtcClientTickTimer = <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

  <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">// 小程序集成</span><br><span class="line"><span class="tag">&lt;<span class="name">live-pusher</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">url</span>=<span class="string">&quot;&#123;&#123;pushUrl&#125;&#125;&quot;</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">autopush</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">mode</span>=<span class="string">&quot;RTC&quot;</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">enable-camera</span>=<span class="string">&quot;&#123;&#123;enableCamera&#125;&#125;&quot;</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">device-position</span>=<span class="string">&quot;&#123;&#123;devicePosition&#125;&#125;&quot;</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">waiting-image</span>=<span class="string">&quot;/images/connecting.jpg&quot;</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">background-mute</span>=<span class="string">&quot;&#123;&#123;false&#125;&#125;&quot;</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">auto-pause-if-open-native</span>=<span class="string">&quot;&#123;&#123;false&#125;&#125;&quot;</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">bindstatechange</span>=<span class="string">&quot;handlePusherStateChange&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">bindnetstatus</span>=<span class="string">&quot;handlePusherNetStatusChange&quot;</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">binderror</span>=<span class="string">&quot;handlePusherError&quot;</span></span></span><br><span class="line"><span class="tag">/&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">live-player</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">hidden</span>=<span class="string">&quot;&#123;&#123;!enableCamera&#125;&#125;&quot;</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">src</span>=<span class="string">&quot;&#123;&#123;playUrl&#125;&#125;&quot;</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">mode</span>=<span class="string">&quot;RTC&quot;</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">autoplay</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">sound-mode</span>=<span class="string">&quot;speaker&quot;</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">object-fit</span>=<span class="string">&quot;fillCrop&quot;</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">auto-pause-if-open-native</span>=<span class="string">&quot;&#123;&#123;false&#125;&#125;&quot;</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">bindstatechange</span>=<span class="string">&quot;handlePlayerStateChange&quot;</span> </span></span><br><span class="line"><span class="tag">    <span class="attr">bindnetstatus</span>=<span class="string">&quot;handlePlayerNetStatusChange&quot;</span></span></span><br><span class="line"><span class="tag">/&gt;</span></span><br></pre></td></tr></table></figure>




    </div>

    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;margin-top:20px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>chuangges
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://chuangges.gitee.io/2019/03/02/2-4-web%E5%9B%BE%E5%83%8F/" title="Web 图片、二维码、视频">https://chuangges.gitee.io/2019/03/02/2-4-web图像/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh_CN" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2019/02/23/2-3-web%E5%B7%A5%E5%85%B7/" rel="prev" title="web 开发工具">
      <i class="fa fa-chevron-left"></i> web 开发工具
    </a></div>
      <div class="post-nav-item">
    <a href="/2019/05/06/3-1-html/" rel="next" title="HTML 元素">
      HTML 元素 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81Web-%E5%9B%BE%E7%89%87"><span class="nav-number">1.</span> <span class="nav-text">一、Web 图片</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%BE%E7%89%87%E8%8E%B7%E5%8F%96"><span class="nav-number">1.1.</span> <span class="nav-text">图片获取</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8B%BC%E5%9B%BE%E9%AA%8C%E8%AF%81"><span class="nav-number">1.2.</span> <span class="nav-text">拼图验证</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%BE%E7%89%87%E6%87%92%E5%8A%A0%E8%BD%BD"><span class="nav-number">1.3.</span> <span class="nav-text">图片懒加载</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ThreeJS-%E7%BB%98%E5%9B%BE"><span class="nav-number">1.4.</span> <span class="nav-text">ThreeJS 绘图</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81JS-%E5%AE%89%E5%8D%93%E6%89%AB%E7%A0%81"><span class="nav-number">2.</span> <span class="nav-text">二、JS 安卓扫码</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Cordova-%E7%9B%91%E5%90%AC"><span class="nav-number">2.1.</span> <span class="nav-text">Cordova 监听</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#WebRTC-%E8%87%AA%E5%8A%A8"><span class="nav-number">2.2.</span> <span class="nav-text">WebRTC 自动</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81Video-%E8%A7%86%E9%A2%91"><span class="nav-number">3.</span> <span class="nav-text">三、Video 视频</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%84%E4%BB%B6%E5%B0%81%E8%A3%85"><span class="nav-number">3.1.</span> <span class="nav-text">组件封装</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%84%E4%BB%B6%E4%BD%BF%E7%94%A8"><span class="nav-number">3.2.</span> <span class="nav-text">组件使用</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81Agora-%E5%A3%B0%E7%BD%91"><span class="nav-number">4.</span> <span class="nav-text">四、Agora 声网</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#RTM-%E5%AE%9E%E6%97%B6%E6%B6%88%E6%81%AF"><span class="nav-number">4.1.</span> <span class="nav-text">RTM 实时消息</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#RTC-%E5%AE%9E%E6%97%B6%E9%9F%B3%E8%A7%86%E9%A2%91"><span class="nav-number">4.2.</span> <span class="nav-text">RTC 实时音视频</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%A1%B9%E7%9B%AE%E9%9B%86%E6%88%90"><span class="nav-number">4.3.</span> <span class="nav-text">项目集成</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="chuangges" src="" data-original="/images/chuang.png">
  <p class="site-author-name" itemprop="name">chuangges</p>
  <div class="site-description" itemprop="description">chuangge's blog.</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">39</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/chuangges" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;chuangges" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://stackoverflow.com/yourname" title="StackOverflow → https:&#x2F;&#x2F;stackoverflow.com&#x2F;yourname" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-stack-overflow fa-fw"></i>StackOverflow</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://juejin.im/" title="https:&#x2F;&#x2F;juejin.im&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">掘金</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.nowcoder.com/" title="https:&#x2F;&#x2F;www.nowcoder.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">牛客网</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.docschina.org/" title="https:&#x2F;&#x2F;www.docschina.org&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">印记中文</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://tool.lu/" title="http:&#x2F;&#x2F;tool.lu&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">在线工具</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://tinypng.com/" title="https:&#x2F;&#x2F;tinypng.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">图片压缩</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://bigjpg.com/" title="https:&#x2F;&#x2F;bigjpg.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">图片放大</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://tool.chinaz.com/" title="https:&#x2F;&#x2F;tool.chinaz.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">站长工具</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://learn-anything.xyz/" title="https:&#x2F;&#x2F;learn-anything.xyz&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">Learn-Anything</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://stackoverflow.com/" title="https:&#x2F;&#x2F;stackoverflow.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">Stack-Overflow</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">chuangge</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">1m</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">15:14</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="external nofollow noopener noreferrer" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="external nofollow noopener noreferrer" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/tororo.model.json"},"display":{"position":"left","width":180,"height":360},"mobile":{"show":false},"react":{"opacityDefault":1,"opacityOnHover":1},"log":false,"tagMode":false});</script>
        <style>
            [bg-lazy] {
                background-image: none !important;
                background-color: #eee !important;
            }
        </style>
        <script>
            window.imageLazyLoadSetting = {
                isSPA: false,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(e){e.imageLazyLoadSetting.processImages=t;var n=e.imageLazyLoadSetting.isSPA,i=e.imageLazyLoadSetting.preloadRatio||1,r=o();function o(){var t=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")),e=Array.prototype.slice.call(document.querySelectorAll("[bg-lazy]"));return t.concat(e)}function t(){n&&(r=o());for(var t,a=0;a<r.length;a++)0<=(t=(t=r[a]).getBoundingClientRect()).bottom&&0<=t.left&&t.top<=(e.innerHeight*i||document.documentElement.clientHeight*i)&&function(){var t,e,n,i,o=r[a];e=function(){r=r.filter(function(t){return o!==t})},(t=o).hasAttribute("bg-lazy")?(t.removeAttribute("bg-lazy"),e&&e()):(n=new Image,i=t.getAttribute("data-original"),n.onload=function(){t.src=i,t.removeAttribute("data-original"),e&&e()},t.src!==i&&(n.src=i))}()}function a(){clearTimeout(t.tId),t.tId=setTimeout(t,500)}t(),document.addEventListener("scroll",a),e.addEventListener("resize",a),e.addEventListener("orientationchange",a)}(this);</script></body>
</html>
